library(tidyverse) # for data wrangling etc
library(rstanarm) # for fitting models in STAN
library(cmdstanr) # for cmdstan
library(brms) # for fitting models in STAN
library(standist) # for exploring distributions
library(HDInterval) # for HPD intervals
library(posterior) # for posterior draws
library(coda) # for diagnostics
library(bayesplot) # for diagnostics
library(ggmcmc) # for MCMC diagnostics
library(DHARMa) # for residual diagnostics
library(rstan) # for interfacing with STAN
library(emmeans) # for marginal means etc
library(broom) # for tidying outputs
library(tidybayes) # for more tidying outputs
library(ggeffects) # for partial plots
library(broom.mixed) # for summarising models
library(ggeffects) # for partial effects plots
library(bayestestR) # for ROPE
library(see) # for some plots
library(easystats) # for the easystats ecosystem
library(ggridges) # for ridge plots
library(patchwork) # for multiple plots
library(modelsummary) # for data and model summaries
theme_set(theme_grey()) # put the default ggplot theme back
source("helperFunctions.R")Bayesian GLM Part6
1 Preparations
Load the necessary libraries
2 Scenario
An ecologist studying a rocky shore at Phillip Island, in southeastern Australia, was interested in how clumps of intertidal mussels are maintained (Quinn 1988). In particular, he wanted to know how densities of adult mussels affected recruitment of young individuals from the plankton. As with most marine invertebrates, recruitment is highly patchy in time, so he expected to find seasonal variation, and the interaction between season and density - whether effects of adult mussel density vary across seasons - was the aspect of most interest.
The data were collected from four seasons, and with two densities of adult mussels. The experiment consisted of clumps of adult mussels attached to the rocks. These clumps were then brought back to the laboratory, and the number of baby mussels recorded. There were 3-6 replicate clumps for each density and season combination.
| SEASON | DENSITY | RECRUITS | SQRTRECRUITS | GROUP |
|---|---|---|---|---|
| Spring | Low | 15 | 3.87 | SpringLow |
| .. | .. | .. | .. | .. |
| Spring | High | 11 | 3.32 | SpringHigh |
| .. | .. | .. | .. | .. |
| Summer | Low | 21 | 4.58 | SummerLow |
| .. | .. | .. | .. | .. |
| Summer | High | 34 | 5.83 | SummerHigh |
| .. | .. | .. | .. | .. |
| Autumn | Low | 14 | 3.74 | AutumnLow |
| .. | .. | .. | .. | .. |
| SEASON | Categorical listing of Season in which mussel clumps were collected independent variable |
| DENSITY | Categorical listing of the density of mussels within mussel clump independent variable |
| RECRUITS | The number of mussel recruits response variable |
| SQRTRECRUITS | Square root transformation of RECRUITS - needed to meet the test assumptions |
| GROUPS | Categorical listing of Season/Density combinations - used for checking ANOVA assumptions |
3 Read in the data
Rows: 42 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): SEASON, DENSITY, GROUP
dbl (2): RECRUITS, SQRTRECRUITS
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 42
Columns: 5
$ SEASON <chr> "Spring", "Spring", "Spring", "Spring", "Spring", "Spring…
$ DENSITY <chr> "Low", "Low", "Low", "Low", "Low", "High", "High", "High"…
$ RECRUITS <dbl> 15, 10, 13, 13, 5, 11, 10, 15, 10, 13, 1, 21, 31, 21, 18,…
$ SQRTRECRUITS <dbl> 3.872983, 3.162278, 3.605551, 3.605551, 2.236068, 3.31662…
$ GROUP <chr> "SpringLow", "SpringLow", "SpringLow", "SpringLow", "Spri…
# A tibble: 6 × 5
SEASON DENSITY RECRUITS SQRTRECRUITS GROUP
<chr> <chr> <dbl> <dbl> <chr>
1 Spring Low 15 3.87 SpringLow
2 Spring Low 10 3.16 SpringLow
3 Spring Low 13 3.61 SpringLow
4 Spring Low 13 3.61 SpringLow
5 Spring Low 5 2.24 SpringLow
6 Spring High 11 3.32 SpringHigh
spc_tbl_ [42 × 5] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ SEASON : chr [1:42] "Spring" "Spring" "Spring" "Spring" ...
$ DENSITY : chr [1:42] "Low" "Low" "Low" "Low" ...
$ RECRUITS : num [1:42] 15 10 13 13 5 11 10 15 10 13 ...
$ SQRTRECRUITS: num [1:42] 3.87 3.16 3.61 3.61 2.24 ...
$ GROUP : chr [1:42] "SpringLow" "SpringLow" "SpringLow" "SpringLow" ...
- attr(*, "spec")=
.. cols(
.. SEASON = col_character(),
.. DENSITY = col_character(),
.. RECRUITS = col_double(),
.. SQRTRECRUITS = col_double(),
.. GROUP = col_character()
.. )
- attr(*, "problems")=<externalptr>
quinn (42 rows and 5 variables, 5 shown)
ID | Name | Type | Missings | Values | N
---+--------------+-----------+----------+------------+-----------
1 | SEASON | character | 0 (0.0%) | Autumn | 10 (23.8%)
| | | | Spring | 11 (26.2%)
| | | | Summer | 12 (28.6%)
| | | | Winter | 9 (21.4%)
---+--------------+-----------+----------+------------+-----------
2 | DENSITY | character | 0 (0.0%) | High | 24 (57.1%)
| | | | Low | 18 (42.9%)
---+--------------+-----------+----------+------------+-----------
3 | RECRUITS | numeric | 0 (0.0%) | [0, 69] | 42
---+--------------+-----------+----------+------------+-----------
4 | SQRTRECRUITS | numeric | 0 (0.0%) | [0, 8.31] | 42
---+--------------+-----------+----------+------------+-----------
5 | GROUP | character | 0 (0.0%) | AutumnHigh | 6 (14.3%)
| | | | AutumnLow | 4 ( 9.5%)
| | | | SpringHigh | 6 (14.3%)
| | | | SpringLow | 5 (11.9%)
| | | | SummerHigh | 6 (14.3%)
| | | | SummerLow | 6 (14.3%)
| | | | WinterHigh | 6 (14.3%)
| | | | WinterLow | 3 ( 7.1%)
------------------------------------------------------------------
| Unique | Missing Pct. | Mean | SD | Min | Median | Max | Histogram | |
|---|---|---|---|---|---|---|---|---|
| RECRUITS | 25 | 0 | 18.3 | 15.7 | 0.0 | 13.5 | 69.0 | |
| SQRTRECRUITS | 25 | 0 | 3.9 | 1.9 | 0.0 | 3.7 | 8.3 | |
| N | % | |||||||
| SEASON | Autumn | 10 | 23.8 | |||||
| Spring | 11 | 26.2 | ||||||
| Summer | 12 | 28.6 | ||||||
| Winter | 9 | 21.4 | ||||||
| DENSITY | High | 24 | 57.1 | |||||
| Low | 18 | 42.9 | ||||||
| GROUP | AutumnHigh | 6 | 14.3 | |||||
| AutumnLow | 4 | 9.5 | ||||||
| SpringHigh | 6 | 14.3 | ||||||
| SpringLow | 5 | 11.9 | ||||||
| SummerHigh | 6 | 14.3 | ||||||
| SummerLow | 6 | 14.3 | ||||||
| WinterHigh | 6 | 14.3 | ||||||
| WinterLow | 3 | 7.1 |
quinn |>
dplyr::select(-SQRTRECRUITS) |>
modelsummary::datasummary_skim(
type = "numeric",
by = c("SEASON", "DENSITY")
)| SEASON | DENSITY | Unique | Missing Pct. | Mean | SD | Min | Median | Max | Histogram | |
|---|---|---|---|---|---|---|---|---|---|---|
| RECRUITS | Spring | Low | 4 | 0 | 11.2 | 3.9 | 5.0 | 13.0 | 15.0 | |
| Spring | High | 5 | 0 | 10.0 | 4.8 | 1.0 | 10.5 | 15.0 | ||
| Summer | Low | 5 | 0 | 22.0 | 6.1 | 14.0 | 21.0 | 31.0 | ||
| Summer | High | 6 | 0 | 48.2 | 15.0 | 28.0 | 51.5 | 69.0 | ||
| Autumn | Low | 4 | 0 | 18.2 | 3.1 | 14.0 | 19.0 | 21.0 | ||
| Autumn | High | 5 | 0 | 19.7 | 11.9 | 4.0 | 17.5 | 36.0 | ||
| Winter | Low | 2 | 0 | 2.7 | 4.6 | 0.0 | 0.0 | 8.0 | ||
| Winter | High | 5 | 0 | 5.7 | 3.3 | 1.0 | 5.0 | 10.0 |
4 Exploratory data analysis
Model formula: \[ \begin{align} y_i &\sim{} \mathcal{NB}(\lambda_i, \theta)\\ ln(\mu_i) &= \beta_0 + \sum_{j=1}^nT_{[i],j}.\beta_j\\ \beta_0 &\sim{} \mathcal{N}(2.4, 1.5)\\ \beta_{[1,2,3]} &\sim{} \mathcal{N}(0, 1)\\ \end{align} \]
where \(\beta_{0}\) is the y-intercept (mean of the first group), \(\beta_{[1,2,3]}\) are the vector of effects parameters (contrasting each group mean to that of the first group and \(T{[i],j}\) represents a \(i\) by \(j\) model matrix is a model matrix representing the season, density and their interaction on mussel recruitment.
5 Exploratory data analysis
The exploratory data analyses that we performed in the frequentist instalment of this example are equally valid here. That is, boxplots and/or violin plots for each population (substrate type).
# A tibble: 6 × 6
SEASON DENSITY RECRUITS SQRTRECRUITS GROUP fSEASON
<chr> <fct> <dbl> <dbl> <chr> <fct>
1 Spring Low 15 3.87 SpringLow Spring
2 Spring Low 10 3.16 SpringLow Spring
3 Spring Low 13 3.61 SpringLow Spring
4 Spring Low 13 3.61 SpringLow Spring
5 Spring Low 5 2.24 SpringLow Spring
6 Spring High 11 3.32 SpringHigh Spring
Conclusions:
- there is clearly a relationship between mean and variance (as would be expected with the a Poisson
- evidently there are numerous zeros in the Winter/Low group # Fit the model
In rstanarm, the default priors are designed to be weakly informative. They are chosen to provide moderate regularisation (to help prevent over-fitting) and help stabilise the computations.
Priors for model 'quinn.rstanarmP'
------
Intercept (after predictors centered)
~ normal(location = 0, scale = 2.5)
Coefficients
Specified prior:
~ normal(location = [0,0,0,...], scale = [2.5,2.5,2.5,...])
Adjusted prior:
~ normal(location = [0,0,0,...], scale = [5.47,5.80,6.02,...])
------
See help('prior_summary.stanreg') for more details
This tells us:
for the intercept, when the family is Poisson, it is using a normal prior with a mean of 0 and a standard deviation of 2.5. The 2.5 is used for all intercepts. It is often scaled, but only if it is larger than 2.5 is the scaled version kept.
for the coefficients (in this case, the individual effects), the default prior is a normal prior centred around 0 with a standard deviations of 5.47, 5/8, 6.02 etc. This is then adjusted for the scale of the data by dividing the 2.5 by the standard deviation of the numerical dummy variables for the predictor (then rounded).
fSEASONSummer fSEASONAutumn fSEASONWinter
5.467708 5.799380 6.019749
DENSITYLow fSEASONSummer:DENSITYLow fSEASONAutumn:DENSITYLow
4.991312 7.058781 8.414625
fSEASONWinter:DENSITYLow
9.590995
- there is no auxiliary prior as we are employing a Poisson distribution.
Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
quinn.rstanarm1 |>
ggpredict(~ fSEASON + DENSITY) |>
plot(show_data = TRUE) |>
wrap_plots() &
scale_y_log10()Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Warning in scale_y_log10(): log-10 transformation introduced infinite values.
## although, since there are zeros...
quinn.rstanarm1 |>
ggpredict(~ fSEASON + DENSITY) |>
plot(show_data = TRUE, jitter = FALSE) |>
wrap_plots() &
scale_y_continuous(trans = scales::pseudo_log_trans())Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
quinn.rstanarm1 |>
ggemmeans(~ fSEASON + DENSITY) |>
plot(show_data = TRUE) |>
plot(show_data = TRUE) |>
wrap_plots() &
scale_y_log10()Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Warning in scale_y_log10(): log-10 transformation introduced infinite values.
## although, since there are zeros...
quinn.rstanarm1 |>
ggemmeans(~ fSEASON + DENSITY) |>
plot(show_data = TRUE, jitter = FALSE) |>
wrap_plots() &
scale_y_continuous(trans = scales::pseudo_log_trans())Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Conclusions:
- we see that the range of predictions is fairly wide and the predicted means could range from 0 to very large (perhaps too large).
The following link provides some guidance about defining priors. [https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations]
When defining our own priors, we typically do not want them to be scaled.
If we wanted to define our own priors that were less vague, yet still not likely to bias the outcomes, we could try the following priors (mainly plucked out of thin air):
- \(\beta_0\): normal centred at 2.3 with a standard deviation of 5
- \(\beta_1\): normal centred at 0 with a standard deviation of 2
Remember the above are applied on the link scale.
I will also overlay the raw data for comparison.
quinn |>
group_by(fSEASON, DENSITY) |>
summarise(
Mean = log(mean(RECRUITS)),
SD = log(sd(RECRUITS))
)`summarise()` has grouped output by 'fSEASON'. You can override using the
`.groups` argument.
# A tibble: 8 × 4
# Groups: fSEASON [4]
fSEASON DENSITY Mean SD
<fct> <fct> <dbl> <dbl>
1 Spring High 2.30 1.57
2 Spring Low 2.42 1.36
3 Summer High 3.87 2.71
4 Summer Low 3.09 1.81
5 Autumn High 2.98 2.48
6 Autumn Low 2.90 1.13
7 Winter High 1.73 1.20
8 Winter Low 0.981 1.53
(Intercept) fSEASONSummer fSEASONAutumn
Inf 6.024998 6.390475
fSEASONWinter DENSITYLow fSEASONSummer:DENSITYLow
6.633304 5.500045 7.778238
fSEASONAutumn:DENSITYLow fSEASONWinter:DENSITYLow
9.272276 10.568545
Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
quinn.rstanarm2 |>
ggpredict(~ fSEASON + DENSITY) |>
plot(show_data = TRUE) |>
wrap_plots() &
scale_y_log10()Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Warning in scale_y_log10(): log-10 transformation introduced infinite values.
## although, since there are zeros...
quinn.rstanarm2 |>
ggpredict(~ fSEASON + DENSITY) |>
plot(show_data = TRUE, jitter = FALSE) |>
wrap_plots() &
scale_y_continuous(trans = scales::pseudo_log_trans())Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
quinn.rstanarm2 |>
ggemmeans(~ fSEASON + DENSITY) |>
plot(show_data = TRUE) |>
plot(show_data = TRUE) |>
wrap_plots() &
scale_y_log10()Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Warning in scale_y_log10(): log-10 transformation introduced infinite values.
## although, since there are zeros...
quinn.rstanarm2 |>
ggemmeans(~ fSEASON + DENSITY) |>
plot(show_data = TRUE, jitter = FALSE) |>
wrap_plots() &
scale_y_continuous(trans = scales::pseudo_log_trans())Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Now lets refit, conditioning on the data.
In brms, the default priors are designed to be weakly informative. They are chosen to provide moderate regularisation (to help prevent over-fitting) and help stabilise the computations.
Unlike rstanarm, brms models must be compiled before they start sampling. For most models, the compilation of the stan code takes around 45 seconds.
prior class coef group resp dpar nlpar lb ub source
(flat) b default
(flat) b DENSITYLow (vectorized)
(flat) b fSEASONAutumn (vectorized)
(flat) b fSEASONAutumn:DENSITYLow (vectorized)
(flat) b fSEASONSummer (vectorized)
(flat) b fSEASONSummer:DENSITYLow (vectorized)
(flat) b fSEASONWinter (vectorized)
(flat) b fSEASONWinter:DENSITYLow (vectorized)
student_t(3, 2.6, 2.5) Intercept default
Remember that the priors are applied on the link (in this case, log) scale.
quinn |>
group_by(fSEASON, DENSITY) |>
summarise(
Mean = mean(RECRUITS),
Median = median(RECRUITS),
MAD = mad(RECRUITS),
SD = sd(RECRUITS)
) |>
mutate(
log(Mean),
log(Median),
log(MAD),
log(SD)
)`summarise()` has grouped output by 'fSEASON'. You can override using the
`.groups` argument.
# A tibble: 8 × 10
# Groups: fSEASON [4]
fSEASON DENSITY Mean Median MAD SD `log(Mean)` `log(Median)` `log(MAD)`
<fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Spring High 10 10.5 2.22 4.82 2.30 2.35 0.799
2 Spring Low 11.2 13 2.97 3.90 2.42 2.56 1.09
3 Summer High 48.2 51.5 15.6 15.0 3.87 3.94 2.75
4 Summer Low 22 21 6.67 6.13 3.09 3.04 1.90
5 Autumn High 19.7 17.5 12.6 11.9 2.98 2.86 2.53
6 Autumn Low 18.2 19 2.22 3.10 2.90 2.94 0.799
7 Winter High 5.67 5 3.71 3.33 1.73 1.61 1.31
8 Winter Low 2.67 0 0 4.62 0.981 -Inf -Inf
# ℹ 1 more variable: `log(SD)` <dbl>
- \(\beta_0\): normal centred at 2.3 with a standard deviation of 1.5
- \(\beta_1\): normal centred at 0 with a standard deviation of 1
priors <- prior(normal(2.4, 1.5), class = "Intercept") +
prior(normal(0, 1), class = "b")
quinn.brm2 <- brm(quinn.form,
data = quinn,
prior = priors,
sample_prior = "only",
refresh = 0,
chains = 3,
iter = 5000,
thin = 5,
warmup = 2500,
backend = "rstan"
)Compiling Stan program...
Start sampling
Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Warning in scale_y_log10(): log-10 transformation introduced infinite values.
## Or since there are zeros
quinn.brm2 |>
ggpredict(~ fSEASON + DENSITY) |>
plot(show_data = TRUE) +
scale_y_continuous(trans = scales::pseudo_log_trans())Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Warning in scale_y_log10(): log-10 transformation introduced infinite values.
## Or since there are zeros
quinn.brm2 |>
ggemmeans(~ fSEASON + DENSITY) |>
plot(show_data = TRUE) +
scale_y_continuous(trans = scales::pseudo_log_trans())Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
[1] "b_Intercept" "b_fSEASONSummer"
[3] "b_fSEASONAutumn" "b_fSEASONWinter"
[5] "b_DENSITYLow" "b_fSEASONSummer:DENSITYLow"
[7] "b_fSEASONAutumn:DENSITYLow" "b_fSEASONWinter:DENSITYLow"
[9] "Intercept" "prior_Intercept"
[11] "prior_b" "lprior"
[13] "lp__" "accept_stat__"
[15] "stepsize__" "treedepth__"
[17] "n_leapfrog__" "divergent__"
[19] "energy__"
quinn.brmP |>
posterior_samples() |>
dplyr::select(-`lp__`) |>
pivot_longer(everything(), names_to = "key") |>
mutate(
Type = ifelse(str_detect(key, "prior"), "Prior", "b"),
Class = ifelse(str_detect(key, "Intercept"), "Intercept",
ifelse(str_detect(key, "b"), "b", "sigma")
),
Par = str_replace(key, "b_", "")
) |>
ggplot(aes(x = Type, y = value, color = Par)) +
stat_pointinterval(position = position_dodge()) +
facet_wrap(~Class, scales = "free")Warning: Method 'posterior_samples' is deprecated. Please see ?as_draws for
recommended alternatives.
$N
[1] 42
$Y
[1] 15 10 13 13 5 11 10 15 10 13 1 21 31 21 18 14 27 34 49 69 55 28 54 14 18
[26] 20 21 4 22 30 36 13 13 8 0 0 10 1 5 9 4 5
$K
[1] 8
$Kc
[1] 7
$X
Intercept fSEASONSummer fSEASONAutumn fSEASONWinter DENSITYLow
1 1 0 0 0 1
2 1 0 0 0 1
3 1 0 0 0 1
4 1 0 0 0 1
5 1 0 0 0 1
6 1 0 0 0 0
7 1 0 0 0 0
8 1 0 0 0 0
9 1 0 0 0 0
10 1 0 0 0 0
11 1 0 0 0 0
12 1 1 0 0 1
13 1 1 0 0 1
14 1 1 0 0 1
15 1 1 0 0 1
16 1 1 0 0 1
17 1 1 0 0 1
18 1 1 0 0 0
19 1 1 0 0 0
20 1 1 0 0 0
21 1 1 0 0 0
22 1 1 0 0 0
23 1 1 0 0 0
24 1 0 1 0 1
25 1 0 1 0 1
26 1 0 1 0 1
27 1 0 1 0 1
28 1 0 1 0 0
29 1 0 1 0 0
30 1 0 1 0 0
31 1 0 1 0 0
32 1 0 1 0 0
33 1 0 1 0 0
34 1 0 0 1 1
35 1 0 0 1 1
36 1 0 0 1 1
37 1 0 0 1 0
38 1 0 0 1 0
39 1 0 0 1 0
40 1 0 0 1 0
41 1 0 0 1 0
42 1 0 0 1 0
fSEASONSummer:DENSITYLow fSEASONAutumn:DENSITYLow fSEASONWinter:DENSITYLow
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0
6 0 0 0
7 0 0 0
8 0 0 0
9 0 0 0
10 0 0 0
11 0 0 0
12 1 0 0
13 1 0 0
14 1 0 0
15 1 0 0
16 1 0 0
17 1 0 0
18 0 0 0
19 0 0 0
20 0 0 0
21 0 0 0
22 0 0 0
23 0 0 0
24 0 1 0
25 0 1 0
26 0 1 0
27 0 1 0
28 0 0 0
29 0 0 0
30 0 0 0
31 0 0 0
32 0 0 0
33 0 0 0
34 0 0 1
35 0 0 1
36 0 0 1
37 0 0 0
38 0 0 0
39 0 0 0
40 0 0 0
41 0 0 0
42 0 0 0
attr(,"assign")
[1] 0 1 1 1 2 3 3 3
attr(,"contrasts")
attr(,"contrasts")$fSEASON
Summer Autumn Winter
Spring 0 0 0
Summer 1 0 0
Autumn 0 1 0
Winter 0 0 1
attr(,"contrasts")$DENSITY
Low
High 0
Low 1
$prior_only
[1] 0
attr(,"class")
[1] "standata" "list"
// generated with brms 2.22.0
functions {
}
data {
int<lower=1> N; // total number of observations
array[N] int Y; // response variable
int<lower=1> K; // number of population-level effects
matrix[N, K] X; // population-level design matrix
int<lower=1> Kc; // number of population-level effects after centering
int prior_only; // should the likelihood be ignored?
}
transformed data {
matrix[N, Kc] Xc; // centered version of X without an intercept
vector[Kc] means_X; // column means of X before centering
for (i in 2:K) {
means_X[i - 1] = mean(X[, i]);
Xc[, i - 1] = X[, i] - means_X[i - 1];
}
}
parameters {
vector[Kc] b; // regression coefficients
real Intercept; // temporary intercept for centered predictors
}
transformed parameters {
real lprior = 0; // prior contributions to the log posterior
lprior += normal_lpdf(b | 0, 1);
lprior += normal_lpdf(Intercept | 2.4, 1.5);
}
model {
// likelihood including constants
if (!prior_only) {
target += poisson_log_glm_lpmf(Y | Xc, Intercept, b);
}
// priors including constants
target += lprior;
}
generated quantities {
// actual population-level intercept
real b_Intercept = Intercept - dot_product(means_X, b);
// additionally sample draws from priors
real prior_b = normal_rng(0,1);
real prior_Intercept = normal_rng(2.4,1.5);
}
6 MCMC sampling diagnostics
The bayesplot package offers a range of MCMC diagnostics as well as Posterior Probability Checks (PPC), all of which have a convenient plot() interface. Lets start with the MCMC diagnostics.
bayesplot MCMC module:
mcmc_acf
mcmc_acf_bar
mcmc_areas
mcmc_areas_ridges
mcmc_combo
mcmc_dens
mcmc_dens_chains
mcmc_dens_overlay
mcmc_hex
mcmc_hist
mcmc_hist_by_chain
mcmc_intervals
mcmc_neff
mcmc_neff_hist
mcmc_nuts_acceptance
mcmc_nuts_divergence
mcmc_nuts_energy
mcmc_nuts_stepsize
mcmc_nuts_treedepth
mcmc_pairs
mcmc_parcoord
mcmc_rank_ecdf
mcmc_rank_hist
mcmc_rank_overlay
mcmc_recover_hist
mcmc_recover_intervals
mcmc_recover_scatter
mcmc_rhat
mcmc_rhat_hist
mcmc_scatter
mcmc_trace
mcmc_trace_highlight
mcmc_violin
Of these, we will focus on:
- mcmc_trace: this plots the estimates of each parameter over the post-warmup length of each MCMC chain. Each chain is plotted in a different shade of blue, with each parameter in its own facet. Ideally, each trace should just look like noise without any discernible drift and each of the traces for a specific parameter should look the same (i.e, should not be displaced above or below any other trace for that parameter).
The chains appear well mixed and very similar
- acf (auto-correlation function): plots the auto-correlation between successive MCMC sample lags for each parameter and each chain
There is no evidence of auto-correlation in the MCMC samples
- Rhat: Rhat is a measure of convergence between the chains. The closer the values are to 1, the more the chains have converged. Values greater than 1.05 indicate a lack of convergence. There will be an Rhat value for each parameter estimated.
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
All Rhat values are below 1.05, suggesting the chains have converged.
neff (number of effective samples): the ratio of the number of effective samples (those not rejected by the sampler) to the number of samples provides an indication of the effectiveness (and efficiency) of the MCMC sampler. Ratios that are less than 0.5 for a parameter suggest that the sampler spent considerable time in difficult areas of the sampling domain and rejected more than half of the samples (replacing them with the previous effective sample).
If the ratios are low, tightening the priors may help.
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Ratios all very high.
The rstan package offers a range of MCMC diagnostics. Lets start with the MCMC diagnostics.
Of these, we will focus on:
- stan_trace: this plots the estimates of each parameter over the post-warmup length of each MCMC chain. Each chain is plotted in a different colour, with each parameter in its own facet. Ideally, each trace should just look like noise without any discernible drift and each of the traces for a specific parameter should look the same (i.e, should not be displaced above or below any other trace for that parameter).
The chains appear well mixed and very similar
- stan_acf (autocorrelation function): plots the autocorrelation between successive MCMC sample lags for each parameter and each chain
There is no evidence of auto-correlation in the MCMC samples
- stan_rhat: Rhat is a scale reduction factor measure of convergence between the chains. The closer the values are to 1, the more the chains have converged. Values greater than 1.05 indicate a lack of convergence. There will be an Rhat value for each parameter estimated.
All Rhat values are below 1.05, suggesting the chains have converged.
stan_ess (number of effective samples): the ratio of the number of effective samples (those not rejected by the sampler) to the number of samples provides an indication of the effectiveness (and efficiency) of the MCMC sampler. Ratios that are less than 0.5 for a parameter suggest that the sampler spent considerable time in difficult areas of the sampling domain and rejected more than half of the samples (replacing them with the previous effective sample).
If the ratios are low, tightening the priors may help.
Ratios all very high.
The ggmean package also has a set of MCMC diagnostic functions. Lets start with the MCMC diagnostics.
Of these, we will focus on:
- ggs_traceplot: this plots the estimates of each parameter over the post-warmup length of each MCMC chain. Each chain is plotted in a different colour, with each parameter in its own facet. Ideally, each trace should just look like noise without any discernible drift and each of the traces for a specific parameter should look the same (i.e, should not be displaced above or below any other trace for that parameter).
The chains appear well mixed and very similar
- gss_autocorrelation (autocorrelation function): plots the autocorrelation between successive MCMC sample lags for each parameter and each chain
There is no evidence of auto-correlation in the MCMC samples
- stan_rhat: Rhat is a scale reduction factor measure of convergence between the chains. The closer the values are to 1, the more the chains have converged. Values greater than 1.05 indicate a lack of convergence. There will be an Rhat value for each parameter estimated.
All Rhat values are below 1.05, suggesting the chains have converged.
stan_ess (number of effective samples): the ratio of the number of effective samples (those not rejected by the sampler) to the number of samples provides an indication of the effectiveness (and efficiency) of the MCMC sampler. Ratios that are less than 0.5 for a parameter suggest that the sampler spent considerable time in difficult areas of the sampling domain and rejected more than half of the samples (replacing them with the previous effective sample).
If the ratios are low, tightening the priors may help.
Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
dplyr 1.1.0.
ℹ Please use `reframe()` instead.
ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
always returns an ungrouped data frame and adjust accordingly.
ℹ The deprecated feature was likely used in the ggmcmc package.
Please report the issue at <https://github.com/xfim/ggmcmc/issues/>.
Ratios all very high.
The brms package offers a range of MCMC diagnostics. Lets start with the MCMC diagnostics.
Of these, we will focus on:
- stan_trace: this plots the estimates of each parameter over the post-warmup length of each MCMC chain. Each chain is plotted in a different colour, with each parameter in its own facet. Ideally, each trace should just look like noise without any discernible drift and each of the traces for a specific parameter should look the same (i.e, should not be displaced above or below any other trace for that parameter).
'pars' not specified. Showing first 10 parameters by default.
'pars' not specified. Showing first 10 parameters by default.
The chains appear well mixed and very similar
- stan_acf (auto-correlation function): plots the auto-correlation between successive MCMC sample lags for each parameter and each chain
There is no evidence of auto-correlation in the MCMC samples
- stan_rhat: Rhat is a scale reduction factor measure of convergence between the chains. The closer the values are to 1, the more the chains have converged. Values greater than 1.05 indicate a lack of convergence. There will be an Rhat value for each parameter estimated.
All Rhat values are below 1.05, suggesting the chains have converged.
stan_ess (number of effective samples): the ratio of the number of effective samples (those not rejected by the sampler) to the number of samples provides an indication of the effectiveness (and efficiency) of the MCMC sampler. Ratios that are less than 0.5 for a parameter suggest that the sampler spent considerable time in difficult areas of the sampling domain and rejected more than half of the samples (replacing them with the previous effective sample).
If the ratios are low, tightening the priors may help.
Ratios all very high.
7 Model validation
Post predictive checks provide additional diagnostics about the fit of the model. Specifically, they provide a comparison between predictions drawn from the model and the observed data used to train the model.
bayesplot PPC module:
ppc_bars
ppc_bars_grouped
ppc_boxplot
ppc_dens
ppc_dens_overlay
ppc_dens_overlay_grouped
ppc_ecdf_overlay
ppc_ecdf_overlay_grouped
ppc_error_binned
ppc_error_hist
ppc_error_hist_grouped
ppc_error_scatter
ppc_error_scatter_avg
ppc_error_scatter_avg_grouped
ppc_error_scatter_avg_vs_x
ppc_freqpoly
ppc_freqpoly_grouped
ppc_hist
ppc_intervals
ppc_intervals_grouped
ppc_km_overlay
ppc_km_overlay_grouped
ppc_loo_intervals
ppc_loo_pit_ecdf
ppc_loo_pit_overlay
ppc_loo_pit_qq
ppc_loo_ribbon
ppc_pit_ecdf
ppc_pit_ecdf_grouped
ppc_ribbon
ppc_ribbon_grouped
ppc_rootogram
ppc_scatter
ppc_scatter_avg
ppc_scatter_avg_grouped
ppc_stat
ppc_stat_2d
ppc_stat_freqpoly
ppc_stat_freqpoly_grouped
ppc_stat_grouped
ppc_violin_grouped
- dens_overlay: plots the density distribution of the observed data (black line) overlayed on top of 50 density distributions generated from draws from the model (light blue). Ideally, the 50 realisations should be roughly consistent with the observed data.
The model draws are broadly similar to the observed data.
- error_scatter_avg: this plots the observed values against the average residuals. Similar to a residual plot, we do not want to see any patterns in this plot. There is some pattern remaining in these residuals.
The predictive error seems to be related to the predictor - the model performs poorest at higher recruitments
- error_scatter_avg_vs_x: this is similar to a regular residual plot and as such should be interpreted as such.
- intervals: plots the observed data overlayed on top of posterior predictions associated with each level of the predictor. Ideally, the observed data should all fall within the predictive intervals.
The modelled predictions seem to do a reasonable job of representing the observations.
The shinystan package allows the full suite of MCMC diagnostics and posterior predictive checks to be accessed via a web interface.
DHARMa residuals provide very useful diagnostics. Unfortunately, we cannot directly use the simulateResiduals() function to generate the simulated residuals. However, if we are willing to calculate some of the components yourself, we can still obtain the simulated residuals from the fitted stan model.
We need to supply:
- simulated (predicted) responses associated with each observation.
- observed values
- fitted (predicted) responses (averaged) associated with each observation
preds <- posterior_predict(quinn.rstanarm3, nsamples = 250, summary = FALSE)
quinn.resids <- createDHARMa(
simulatedResponse = t(preds),
observedResponse = quinn$RECRUITS,
fittedPredictedResponse = apply(preds, 2, median),
integerResponse = TRUE
)
plot(quinn.resids)Conclusions:
- the simulated residuals suggest there might be an issue of dispersion.
- it might be worth exploring either zero-inflation, a negative binomial model, or including a observation-level random effect.
Post predictive checks provide additional diagnostics about the fit of the model. Specifically, they provide a comparison between predictions drawn from the model and the observed data used to train the model.
bayesplot PPC module:
ppc_bars
ppc_bars_grouped
ppc_boxplot
ppc_dens
ppc_dens_overlay
ppc_dens_overlay_grouped
ppc_ecdf_overlay
ppc_ecdf_overlay_grouped
ppc_error_binned
ppc_error_hist
ppc_error_hist_grouped
ppc_error_scatter
ppc_error_scatter_avg
ppc_error_scatter_avg_grouped
ppc_error_scatter_avg_vs_x
ppc_freqpoly
ppc_freqpoly_grouped
ppc_hist
ppc_intervals
ppc_intervals_grouped
ppc_km_overlay
ppc_km_overlay_grouped
ppc_loo_intervals
ppc_loo_pit_ecdf
ppc_loo_pit_overlay
ppc_loo_pit_qq
ppc_loo_ribbon
ppc_pit_ecdf
ppc_pit_ecdf_grouped
ppc_ribbon
ppc_ribbon_grouped
ppc_rootogram
ppc_scatter
ppc_scatter_avg
ppc_scatter_avg_grouped
ppc_stat
ppc_stat_2d
ppc_stat_freqpoly
ppc_stat_freqpoly_grouped
ppc_stat_grouped
ppc_violin_grouped
- dens_overlay: plots the density distribution of the observed data (black line) overlayed on top of 50 density distributions generated from draws from the model (light blue). Ideally, the 50 realisations should be roughly consistent with the observed data.
The model draws appear to represent the shape of the observed data reasonably well
- error_scatter_avg: this plots the observed values against the average residuals. Similar to a residual plot, we do not want to see any patterns in this plot. There is some pattern remaining in these residuals.
Using all posterior draws for ppc type 'error_scatter_avg' by default.
The predictive error seems to be related to the predictor - the model performs poorest at higher recruitments.
- intervals: plots the observed data overlayed on top of posterior predictions associated with each level of the predictor. Ideally, the observed data should all fall within the predictive intervals.
Using all posterior draws for ppc type 'intervals' by default.
Whilst the modelled predictions do a reasonable job of representing the observed data, the observed data do appear to be more varied than the model is representing.
The shinystan package allows the full suite of MCMC diagnostics and posterior predictive checks to be accessed via a web interface.
DHARMa residuals provide very useful diagnostics. Unfortunately, we cannot directly use the simulateResiduals() function to generate the simulated residuals. However, if we are willing to calculate some of the components yourself, we can still obtain the simulated residuals from the fitted stan model.
We need to supply:
- simulated (predicted) responses associated with each observation.
- observed values
- fitted (predicted) responses (averaged) associated with each observation
Warning: Argument 'nsamples' is deprecated. Please use argument 'ndraws'
instead.
quinn.resids <- createDHARMa(
simulatedResponse = t(preds),
observedResponse = quinn$RECRUITS,
fittedPredictedResponse = apply(preds, 2, median),
integerResponse = TRUE
)
quinn.resids |> plot()
DHARMa nonparametric dispersion test via sd of residuals fitted vs.
simulated
data: simulationOutput
dispersion = 2.6852, p-value = 0.008
alternative hypothesis: two.sided
DHARMa zero-inflation test via comparison to expected zeros with
simulation under H0 = fitted model
data: simulationOutput
ratioObsSim = 6.5789, p-value = 0.08
alternative hypothesis: two.sided
quinn.resids <- make_brms_dharma_res(quinn.brmP, integerResponse = TRUE)
wrap_elements(~ testUniformity(quinn.resids)) +
wrap_elements(~ plotResiduals(quinn.resids, form = factor(rep(1, nrow(quinn))))) +
wrap_elements(~ plotResiduals(quinn.resids, quantreg = TRUE)) +
wrap_elements(~ testDispersion(quinn.resids))Conclusions:
- the simulated residuals do suggest that there might be a dispersion issue
- it might be worth exploring either zero-inflation, a negative binomial model, or including a observation-level random effect.
8 Explore negative binomial model
quinn.rstanarmNB <- stan_glm(RECRUITS ~ fSEASON * DENSITY,
data = quinn,
family = neg_binomial_2(link = "log"),
prior_intercept = normal(2.3, 2, autoscale = FALSE),
prior = normal(0, 10, autoscale = FALSE),
prior_aux = rstanarm::exponential(rate = 1, autoscale = FALSE),
prior_PD = FALSE,
refresh = 0,
chains = 3, iter = 5000, thin = 5, warmup = 2000
)Data points may overlap. Use the `jitter` argument to add some amount of
random variation to the location of data points and avoid overplotting.
There seems to be a bug here. The expected values should be being back transformed to the response scale, however, they are clearly not. Notice that the expected values (and associated CI) are low and tiny respectively).
preds <- posterior_predict(quinn.rstanarmNB, nsamples = 250, summary = FALSE)
quinn.resids <- createDHARMa(
simulatedResponse = t(preds),
observedResponse = quinn$RECRUITS,
fittedPredictedResponse = apply(preds, 2, median),
integerResponse = TRUE
)
plot(quinn.resids)
DHARMa nonparametric dispersion test via sd of residuals fitted vs.
simulated
data: simulationOutput
dispersion = 0.27522, p-value = 0.03222
alternative hypothesis: two.sided
Now possibly under-dispersed..
Warning: Found 2 observation(s) with a pareto_k > 0.7. We recommend calling 'loo' again with argument 'k_threshold = 0.7' in order to calculate the ELPD without the assumption that these observations are negligible. This will refit the model 2 times to compute the ELPDs for the problematic observations directly.
Computed from 1800 by 42 log-likelihood matrix.
Estimate SE
elpd_loo -171.0 15.7
p_loo 24.3 5.7
looic 342.0 31.3
------
MCSE of elpd_loo is NA.
MCSE and ESS estimates assume MCMC draws (r_eff in [0.8, 1.1]).
Pareto k diagnostic values:
Count Pct. Min. ESS
(-Inf, 0.69] (good) 40 95.2% 161
(0.69, 1] (bad) 1 2.4% <NA>
(1, Inf) (very bad) 1 2.4% <NA>
See help('pareto-k-diagnostic') for details.
Warning: Found 1 observation(s) with a pareto_k > 0.7. We recommend calling 'loo' again with argument 'k_threshold = 0.7' in order to calculate the ELPD without the assumption that these observations are negligible. This will refit the model 1 times to compute the ELPDs for the problematic observations directly.
Computed from 1800 by 42 log-likelihood matrix.
Estimate SE
elpd_loo -150.4 6.0
p_loo 9.3 3.5
looic 300.9 12.0
------
MCSE of elpd_loo is NA.
MCSE and ESS estimates assume MCMC draws (r_eff in [0.9, 1.1]).
Pareto k diagnostic values:
Count Pct. Min. ESS
(-Inf, 0.69] (good) 41 97.6% 622
(0.69, 1] (bad) 0 0.0% <NA>
(1, Inf) (very bad) 1 2.4% <NA>
See help('pareto-k-diagnostic') for details.
elpd_diff se_diff
quinn.rstanarmNB 0.0 0.0
quinn.rstanarmP -20.5 11.9
quinn.form <- bf(RECRUITS ~ fSEASON * DENSITY, family = negbinomial(link = "log"))
get_prior(quinn.form, data = quinn) prior class coef group resp dpar
(flat) b
(flat) b DENSITYLow
(flat) b fSEASONAutumn
(flat) b fSEASONAutumn:DENSITYLow
(flat) b fSEASONSummer
(flat) b fSEASONSummer:DENSITYLow
(flat) b fSEASONWinter
(flat) b fSEASONWinter:DENSITYLow
student_t(3, 2.6, 2.5) Intercept
inv_gamma(0.4, 0.3) shape
nlpar lb ub source
default
(vectorized)
(vectorized)
(vectorized)
(vectorized)
(vectorized)
(vectorized)
(vectorized)
default
0 default
priors <- prior(normal(2.4, 1.5), class = "Intercept") +
prior(normal(0, 1), class = "b") +
prior(gamma(0.01, 0.01), class = "shape")
quinn.brmsNB <- brm(quinn.form,
data = quinn,
prior = priors,
refresh = 0,
chains = 3,
iter = 5000,
thin = 5,
warmup = 2500,
backend = "rstan"
)Compiling Stan program...
Start sampling
Warning: Argument 'nsamples' is deprecated. Please use argument 'ndraws'
instead.
quinn.resids <- createDHARMa(
simulatedResponse = t(preds),
observedResponse = quinn$RECRUITS,
fittedPredictedResponse = apply(preds, 2, median),
integerResponse = TRUE
)
plot(quinn.resids)quinn.resids <- make_brms_dharma_res(quinn.brmsNB, integerResponse = TRUE)
wrap_elements(~ testUniformity(quinn.resids)) +
## wrap_elements(~plotResiduals(quinn.resids, form = factor(rep(1, nrow(quinn))))) +
wrap_elements(~ plotResiduals(quinn.resids, quantreg = TRUE)) +
wrap_elements(~ testDispersion(quinn.resids))9 Partial effects plots
quinn.rstanarmNB |>
fitted_draws(newdata = quinn) |>
median_hdci() |>
ggplot(aes(x = fSEASON, colour = DENSITY, y = .value)) +
geom_pointrange(aes(ymin = .lower, ymax = .upper), position = position_dodge(width = 0.2)) +
geom_line(position = position_dodge(width = 0.2)) +
geom_point(data = quinn, aes(y = RECRUITS, x = fSEASON, colour = DENSITY), position = position_dodge(width = 0.2))Warning: `fitted_draws` and `add_fitted_draws` are deprecated as their names were confusing.
- Use [add_]epred_draws() to get the expectation of the posterior predictive.
- Use [add_]linpred_draws() to get the distribution of the linear predictor.
- For example, you used [add_]fitted_draws(..., scale = "response"), which
means you most likely want [add_]epred_draws(...).
NOTE: When updating to the new functions, note that the `model` parameter is now
named `object` and the `n` parameter is now named `ndraws`.
quinn.brmsNB |>
fitted_draws(newdata = quinn) |>
median_hdci() |>
ggplot(aes(x = fSEASON, colour = DENSITY, y = .value)) +
geom_pointrange(aes(ymin = .lower, ymax = .upper), position = position_dodge(width = 0.2)) +
geom_line(position = position_dodge(width = 0.2)) +
geom_point(data = quinn, aes(y = RECRUITS, x = fSEASON, colour = DENSITY), position = position_dodge(width = 0.2))Warning: `fitted_draws` and `add_fitted_draws` are deprecated as their names were confusing.
- Use [add_]epred_draws() to get the expectation of the posterior predictive.
- Use [add_]linpred_draws() to get the distribution of the linear predictor.
- For example, you used [add_]fitted_draws(..., scale = "response"), which
means you most likely want [add_]epred_draws(...).
NOTE: When updating to the new functions, note that the `model` parameter is now
named `object` and the `n` parameter is now named `ndraws`.
10 Model investigation
The summary() method generates simple summaries (mean, standard deviation as well as 10, 50 and 90 percentiles).
Model Info:
function: stan_glm
family: neg_binomial_2 [log]
formula: RECRUITS ~ fSEASON * DENSITY
algorithm: sampling
sample: 1800 (posterior sample size)
priors: see help('prior_summary')
observations: 42
predictors: 8
Estimates:
mean sd 10% 50% 90%
(Intercept) 2.3 0.3 2.0 2.3 2.6
fSEASONSummer 1.6 0.3 1.1 1.6 2.0
fSEASONAutumn 0.7 0.3 0.2 0.7 1.1
fSEASONWinter -0.6 0.4 -1.1 -0.6 -0.1
DENSITYLow 0.1 0.4 -0.4 0.1 0.6
fSEASONSummer:DENSITYLow -0.9 0.5 -1.5 -0.9 -0.3
fSEASONAutumn:DENSITYLow -0.2 0.5 -0.8 -0.2 0.5
fSEASONWinter:DENSITYLow -0.9 0.7 -1.7 -0.9 0.0
reciprocal_dispersion 4.0 1.3 2.6 3.8 5.7
Fit Diagnostics:
mean sd 10% 50% 90%
mean_PPD 19.3 3.0 15.7 19.0 23.2
The mean_ppd is the sample average posterior predictive distribution of the outcome variable (for details see help('summary.stanreg')).
MCMC diagnostics
mcse Rhat n_eff
(Intercept) 0.0 1.0 1811
fSEASONSummer 0.0 1.0 1771
fSEASONAutumn 0.0 1.0 1667
fSEASONWinter 0.0 1.0 1872
DENSITYLow 0.0 1.0 1817
fSEASONSummer:DENSITYLow 0.0 1.0 1661
fSEASONAutumn:DENSITYLow 0.0 1.0 1773
fSEASONWinter:DENSITYLow 0.0 1.0 1940
reciprocal_dispersion 0.0 1.0 1574
mean_PPD 0.1 1.0 1703
log-posterior 0.1 1.0 1645
For each parameter, mcse is Monte Carlo standard error, n_eff is a crude measure of effective sample size, and Rhat is the potential scale reduction factor on split chains (at convergence Rhat=1).
Conclusions:
- the intercept represents the estimated mean of the first combination of Season (Spring) and Density (High). On the link scale this is 2.32
- the difference between Low and High adult density in spring is 1.56, although this is not significant
- the difference between Spring and Summer for High adult density is 0.67
- the difference between Spring and Autumn for High adult density is -0.58
- the difference between Spring and Winter for High adult density is 0.12
- if there were no interactions, we would expect the Low density Summer recruitment to be the additive of the main effects (Low and Summer). However, the modelled mean is 0.88 less than the additive effects would have expected. This value is significantly different to 0, indicating that there is evidence that the density effect in Summer is different to that in Spring.
- the density effect in Autumn and Winter were not found to be significantly different from what you would expect from an additive model.
tidyMCMC(quinn.rstanarmNB$stanfit, estimate.method = "median", conf.int = TRUE, conf.method = "HPDinterval", rhat = TRUE, ess = TRUE)# A tibble: 11 × 7
term estimate std.error conf.low conf.high rhat ess
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
1 (Intercept) 2.32 0.251 1.84 2.83 1.00 1811
2 fSEASONSummer 1.56 0.333 0.958 2.23 0.999 1771
3 fSEASONAutumn 0.668 0.342 0.0161 1.35 1.00 1667
4 fSEASONWinter -0.582 0.375 -1.27 0.208 1.00 1872
5 DENSITYLow 0.116 0.368 -0.649 0.794 1.00 1817
6 fSEASONSummer:DENSITYLow -0.885 0.478 -1.76 0.100 1.00 1661
7 fSEASONAutumn:DENSITYLow -0.167 0.512 -1.15 0.914 1.00 1773
8 fSEASONWinter:DENSITYLow -0.888 0.658 -2.12 0.449 1.00 1940
9 reciprocal_dispersion 4.00 1.25 1.81 6.63 1.00 1574
10 mean_PPD 19.3 3.02 13.7 25.0 1.00 1703
11 log-posterior -155. 2.35 -160. -151. 1.00 1645
Conclusions:
See above
quinn.rstanarmNB$stanfit |>
summarise_draws(
median,
HDInterval::hdi,
rhat, length, ess_bulk, ess_tail
)# A tibble: 11 × 8
variable median lower upper rhat length ess_bulk ess_tail
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.31 1.84e+0 2.83 1.00 1800 1824. 1637.
2 fSEASONSummer 1.56 9.58e-1 2.23 0.999 1800 1787. 1652.
3 fSEASONAutumn 0.670 1.61e-2 1.35 1.00 1800 1715. 1674.
4 fSEASONWinter -0.583 -1.27e+0 0.208 1.00 1800 1857. 1670.
5 DENSITYLow 0.114 -6.49e-1 0.794 1.00 1800 1823. 1637.
6 fSEASONSummer:DENS… -0.900 -1.76e+0 0.100 1.00 1800 1667. 1594.
7 fSEASONAutumn:DENS… -0.150 -1.15e+0 0.914 1.00 1800 1783. 1786.
8 fSEASONWinter:DENS… -0.895 -2.12e+0 0.449 1.00 1800 1927. 1333.
9 reciprocal_dispers… 3.79 1.81e+0 6.63 1.00 1800 1659. 1488.
10 mean_PPD 19.0 1.37e+1 25.0 1.00 1800 1736. 1598.
11 log-posterior -155. -1.60e+2 -151. 1.00 1800 1719. 1698.
We can also alter the CI level.
quinn.rstanarmNB$stanfit |>
summarise_draws(
median,
~ HDInterval::hdi(.x, credMass = 0.9),
rhat, length, ess_bulk, ess_tail
)# A tibble: 11 × 8
variable median lower upper rhat length ess_bulk ess_tail
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.31 1.91 2.73e+0 1.00 1800 1824. 1637.
2 fSEASONSummer 1.56 1.06 2.15e+0 0.999 1800 1787. 1652.
3 fSEASONAutumn 0.670 0.109 1.23e+0 1.00 1800 1715. 1674.
4 fSEASONWinter -0.583 -1.19 1.41e-2 1.00 1800 1857. 1670.
5 DENSITYLow 0.114 -0.504 7.23e-1 1.00 1800 1823. 1637.
6 fSEASONSummer:DENS… -0.900 -1.67 -1.27e-1 1.00 1800 1667. 1594.
7 fSEASONAutumn:DENS… -0.150 -1.03 6.48e-1 1.00 1800 1783. 1786.
8 fSEASONWinter:DENS… -0.895 -1.86 2.97e-1 1.00 1800 1927. 1333.
9 reciprocal_dispers… 3.79 2.16 6.06e+0 1.00 1800 1659. 1488.
10 mean_PPD 19.0 14.2 2.38e+1 1.00 1800 1736. 1598.
11 log-posterior -155. -158. -1.52e+2 1.00 1800 1719. 1698.
Arguably, it would be better to back-transform to the ratio scale
quinn.rstanarmNB$stanfit |>
summarise_draws(
~ median(exp(.x)),
~ HDInterval::hdi(exp(.x)),
rhat, length, ess_bulk, ess_tail
)# A tibble: 11 × 8
variable `~median(exp(.x))` lower upper rhat length ess_bulk ess_tail
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 (Interce… 1.01e+ 1 5.70e+ 0 1.59e+ 1 1.00 1800 1824. 1637.
2 fSEASONS… 4.75e+ 0 2.24e+ 0 8.49e+ 0 0.999 1800 1787. 1652.
3 fSEASONA… 1.96e+ 0 8.65e- 1 3.56e+ 0 1.00 1800 1715. 1674.
4 fSEASONW… 5.58e- 1 2.07e- 1 1.05e+ 0 1.00 1800 1857. 1670.
5 DENSITYL… 1.12e+ 0 4.41e- 1 2.07e+ 0 1.00 1800 1823. 1637.
6 fSEASONS… 4.06e- 1 1.08e- 1 9.18e- 1 1.00 1800 1667. 1594.
7 fSEASONA… 8.61e- 1 2.38e- 1 2.06e+ 0 1.00 1800 1783. 1786.
8 fSEASONW… 4.09e- 1 7.12e- 2 1.27e+ 0 1.00 1800 1927. 1333.
9 reciproc… 4.41e+ 1 3.91e+ 0 6.19e+ 2 1.00 1800 1659. 1488.
10 mean_PPD 1.70e+ 8 6.43e+ 4 5.04e+10 1.00 1800 1736. 1598.
11 log-post… 6.51e-68 7.01e-74 9.01e-67 1.00 1800 1719. 1698.
# A draws_df: 600 iterations, 3 chains, and 11 variables
(Intercept) fSEASONSummer fSEASONAutumn fSEASONWinter DENSITYLow
1 2.4 1.6 0.22 -0.69 0.138
2 2.2 1.3 0.96 -0.52 0.097
3 2.6 1.6 0.51 -0.89 -0.559
4 2.5 1.7 0.39 -0.83 -0.113
5 2.4 1.0 0.05 -0.65 -0.194
6 2.3 1.6 0.64 -0.79 0.122
7 2.4 1.4 0.60 -0.38 0.156
8 2.4 1.3 0.58 -0.57 0.191
9 2.3 1.4 0.81 -0.82 0.211
10 2.3 2.0 0.51 -0.27 -0.067
fSEASONSummer:DENSITYLow fSEASONAutumn:DENSITYLow fSEASONWinter:DENSITYLow
1 -1.18 0.401 -0.213
2 -0.90 -0.508 -1.033
3 -0.47 0.063 -0.107
4 -1.11 -0.104 -0.616
5 -0.22 0.841 -0.769
6 -0.59 -0.424 -0.044
7 -1.18 0.213 -1.514
8 -0.78 -0.844 -0.799
9 -0.41 -0.962 -1.039
10 -1.33 0.532 -0.751
# ... with 1790 more draws, and 3 more variables
# ... hidden reserved variables {'.chain', '.iteration', '.draw'}
quinn.rstanarmNB$stanfit |>
as_draws_df() |>
summarise_draws(
median,
~ HDInterval::hdi(.x),
rhat,
ess_bulk
)# A tibble: 11 × 6
variable median lower upper rhat ess_bulk
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.31 1.84 2.83 1.00 1824.
2 fSEASONSummer 1.56 0.958 2.23 0.999 1787.
3 fSEASONAutumn 0.670 0.0161 1.35 1.00 1715.
4 fSEASONWinter -0.583 -1.27 0.208 1.00 1857.
5 DENSITYLow 0.114 -0.649 0.794 1.00 1823.
6 fSEASONSummer:DENSITYLow -0.900 -1.76 0.100 1.00 1667.
7 fSEASONAutumn:DENSITYLow -0.150 -1.15 0.914 1.00 1783.
8 fSEASONWinter:DENSITYLow -0.895 -2.12 0.449 1.00 1927.
9 reciprocal_dispersion 3.79 1.81 6.63 1.00 1659.
10 mean_PPD 19.0 13.7 25.0 1.00 1736.
11 log-posterior -155. -160. -151. 1.00 1719.
quinn.rstanarmNB$stanfit |>
as_draws_df() |>
exp() |>
summarise_draws(
median,
~ HDInterval::hdi(.x),
rhat,
ess_bulk
)# A tibble: 11 × 6
variable median lower upper rhat ess_bulk
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 1.01e+ 1 5.70e+ 0 1.59e+ 1 1.00 1824.
2 fSEASONSummer 4.75e+ 0 2.24e+ 0 8.49e+ 0 0.999 1787.
3 fSEASONAutumn 1.96e+ 0 8.65e- 1 3.56e+ 0 1.00 1715.
4 fSEASONWinter 5.58e- 1 2.07e- 1 1.05e+ 0 1.00 1857.
5 DENSITYLow 1.12e+ 0 4.41e- 1 2.07e+ 0 1.00 1823.
6 fSEASONSummer:DENSITYLow 4.06e- 1 1.08e- 1 9.18e- 1 1.00 1667.
7 fSEASONAutumn:DENSITYLow 8.61e- 1 2.38e- 1 2.06e+ 0 1.00 1783.
8 fSEASONWinter:DENSITYLow 4.09e- 1 7.12e- 2 1.27e+ 0 1.00 1927.
9 reciprocal_dispersion 4.41e+ 1 3.91e+ 0 6.19e+ 2 1.00 1659.
10 mean_PPD 1.70e+ 8 6.43e+ 4 5.04e+10 1.00 1736.
11 log-posterior 6.51e-68 7.01e-74 9.01e-67 1.00 1719.
Due to the presence of a log transform in the predictor, it is better to use the regex version.
[1] "(Intercept)" "fSEASONSummer"
[3] "fSEASONAutumn" "fSEASONWinter"
[5] "DENSITYLow" "fSEASONSummer:DENSITYLow"
[7] "fSEASONAutumn:DENSITYLow" "fSEASONWinter:DENSITYLow"
[9] "reciprocal_dispersion" "accept_stat__"
[11] "stepsize__" "treedepth__"
[13] "n_leapfrog__" "divergent__"
[15] "energy__"
quinn.draw <- quinn.rstanarmNB |> gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE)
quinn.draw# A tibble: 14,400 × 5
# Groups: .variable [8]
.chain .iteration .draw .variable .value
<int> <int> <int> <chr> <dbl>
1 1 1 1 (Intercept) 2.38
2 1 2 2 (Intercept) 2.23
3 1 3 3 (Intercept) 2.55
4 1 4 4 (Intercept) 2.46
5 1 5 5 (Intercept) 2.39
6 1 6 6 (Intercept) 2.28
7 1 7 7 (Intercept) 2.36
8 1 8 8 (Intercept) 2.40
9 1 9 9 (Intercept) 2.29
10 1 10 10 (Intercept) 2.26
# ℹ 14,390 more rows
exceedP <- function(x, Val = 0) mean(x > Val)
quinn.rstanarmNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
mutate(.value = exp(.value)) |>
summarise_draws(
median,
HDInterval::hdi,
rhat,
length,
ess_bulk,
ess_tail,
~ exceedP(.x, 1)
)# A tibble: 8 × 10
# Groups: .variable [8]
.variable variable median lower upper rhat length ess_bulk ess_tail
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) .value 10.1 5.70 15.9 1.00 1800 1824. 1637.
2 DENSITYLow .value 1.12 0.441 2.07 1.00 1800 1823. 1637.
3 fSEASONAutumn .value 1.96 0.865 3.56 1.00 1800 1715. 1674.
4 fSEASONAutumn:DE… .value 0.861 0.238 2.06 1.00 1800 1783. 1786.
5 fSEASONSummer .value 4.75 2.24 8.49 0.999 1800 1787. 1652.
6 fSEASONSummer:DE… .value 0.406 0.108 0.918 1.00 1800 1667. 1594.
7 fSEASONWinter .value 0.558 0.207 1.05 1.00 1800 1857. 1670.
8 fSEASONWinter:DE… .value 0.409 0.0712 1.27 1.00 1800 1927. 1333.
# ℹ 1 more variable: `~exceedP(.x, 1)` <dbl>
We can then summarise this
# A tibble: 8 × 7
.variable .value .lower .upper .width .point .interval
<chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 (Intercept) 2.31 1.81 2.80 0.95 median hdci
2 DENSITYLow 0.114 -0.626 0.820 0.95 median hdci
3 fSEASONAutumn 0.670 0.0161 1.35 0.95 median hdci
4 fSEASONAutumn:DENSITYLow -0.150 -1.29 0.785 0.95 median hdci
5 fSEASONSummer 1.56 0.965 2.24 0.95 median hdci
6 fSEASONSummer:DENSITYLow -0.900 -1.80 0.0689 0.95 median hdci
7 fSEASONWinter -0.583 -1.27 0.208 0.95 median hdci
8 fSEASONWinter:DENSITYLow -0.895 -2.16 0.407 0.95 median hdci
We could alternatively express the parameters on the response scale.
# A tibble: 8 × 7
.variable `exp(.value)` .lower .upper .width .point .interval
<chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 (Intercept) 10.1 5.70 15.9 0.95 median hdci
2 DENSITYLow 1.12 0.466 2.10 0.95 median hdci
3 fSEASONAutumn 1.96 0.788 3.49 0.95 median hdci
4 fSEASONAutumn:DENSITYLow 0.861 0.238 2.06 0.95 median hdci
5 fSEASONSummer 4.75 2.24 8.49 0.95 median hdci
6 fSEASONSummer:DENSITYLow 0.406 0.108 0.918 0.95 median hdci
7 fSEASONWinter 0.558 0.207 1.05 0.95 median hdci
8 fSEASONWinter:DENSITYLow 0.409 0.0835 1.28 0.95 median hdci
quinn.rstanarmNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
stat_halfeye(aes(x = .value, y = .variable)) +
facet_wrap(~.variable, scales = "free")quinn.rstanarmNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
geom_vline(xintercept = 0, linetype = "dashed") +
stat_halfeye(aes(x = .value, y = .variable)) +
theme_classic()We could alternatively express the parameters on the response scale.
quinn.rstanarmNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
group_by(.variable) |>
mutate(.value = exp(.value)) |>
median_hdci()# A tibble: 8 × 7
.variable .value .lower .upper .width .point .interval
<chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 (Intercept) 10.1 5.70 15.9 0.95 median hdci
2 DENSITYLow 1.12 0.466 2.10 0.95 median hdci
3 fSEASONAutumn 1.96 0.788 3.49 0.95 median hdci
4 fSEASONAutumn:DENSITYLow 0.861 0.238 2.06 0.95 median hdci
5 fSEASONSummer 4.75 2.24 8.49 0.95 median hdci
6 fSEASONSummer:DENSITYLow 0.406 0.108 0.918 0.95 median hdci
7 fSEASONWinter 0.558 0.207 1.05 0.95 median hdci
8 fSEASONWinter:DENSITYLow 0.409 0.0835 1.28 0.95 median hdci
quinn.rstanarmNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
mutate(.value = exp(.value)) |>
ggplot() +
geom_vline(xintercept = 1, linetype = "dashed") +
stat_halfeye(aes(x = .value, y = .variable)) +
scale_x_continuous("", trans = scales::log2_trans(), breaks = unique(as.vector(2^(0:4 %o% c(-1, 1))))) +
theme_classic()## Link scale
quinn.rstanarmNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
stat_slab(aes(
x = .value, y = .variable,
fill = stat(ggdist::cut_cdf_qi(cdf,
.width = c(0.5, 0.8, 0.95),
labels = scales::percent_format()
))
), color = "black") +
geom_vline(xintercept = 0, linetype = "dashed") +
scale_fill_brewer("Interval", direction = -1, na.translate = FALSE)Warning: `stat(ggdist::cut_cdf_qi(cdf, .width = c(0.5, 0.8, 0.95), labels =
scales::percent_format()))` was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(ggdist::cut_cdf_qi(cdf, .width = c(0.5, 0.8, 0.95),
labels = scales::percent_format()))` instead.
## Fractional scale
quinn.rstanarmNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
mutate(.value = exp(.value)) |>
ggplot() +
stat_slab(aes(
x = .value, y = .variable,
fill = stat(ggdist::cut_cdf_qi(cdf,
.width = c(0.5, 0.8, 0.95),
labels = scales::percent_format()
))
), color = "black") +
geom_vline(xintercept = 1, linetype = "dashed") +
scale_fill_brewer("Interval", direction = -1, na.translate = FALSE) +
scale_x_continuous(trans = scales::log2_trans())quinn.rstanarmNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
stat_halfeye(aes(x = .value, y = .variable)) +
facet_wrap(~.variable, scales = "free")quinn.rstanarmNB |>
gather_draws(`.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
stat_halfeye(aes(x = .value, y = .variable)) +
geom_vline(xintercept = 0, linetype = "dashed")quinn.rstanarmNB |>
gather_draws(`.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
stat_halfeye(aes(x = exp(.value), y = .variable)) +
geom_vline(xintercept = 1, linetype = "dashed") +
scale_x_continuous(trans = scales::log2_trans())quinn.rstanarmNB |>
gather_draws(`.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
geom_density_ridges(aes(x = .value, y = .variable), alpha = 0.4) +
geom_vline(xintercept = 0, linetype = "dashed")Picking joint bandwidth of 0.0865
## Or on a fractional scale
quinn.rstanarmNB |>
gather_draws(`.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
geom_density_ridges_gradient(
aes(
x = exp(.value),
y = .variable,
fill = stat(x)
),
alpha = 0.4, colour = "white",
quantile_lines = TRUE,
quantiles = c(0.025, 0.975)
) +
geom_vline(xintercept = 1, linetype = "dashed") +
scale_x_continuous(trans = scales::log2_trans()) +
scale_fill_viridis_c(option = "C")Picking joint bandwidth of 0.125
This is purely a graphical depiction on the posteriors.
# A tibble: 1,800 × 18
.chain .iteration .draw `(Intercept)` fSEASONSummer fSEASONAutumn
<int> <int> <int> <dbl> <dbl> <dbl>
1 1 1 1 2.38 1.60 0.221
2 1 2 2 2.23 1.28 0.956
3 1 3 3 2.55 1.60 0.514
4 1 4 4 2.46 1.72 0.389
5 1 5 5 2.39 1.02 0.0498
6 1 6 6 2.28 1.61 0.643
7 1 7 7 2.36 1.36 0.596
8 1 8 8 2.40 1.33 0.580
9 1 9 9 2.29 1.43 0.810
10 1 10 10 2.26 1.99 0.509
# ℹ 1,790 more rows
# ℹ 12 more variables: fSEASONWinter <dbl>, DENSITYLow <dbl>,
# `fSEASONSummer:DENSITYLow` <dbl>, `fSEASONAutumn:DENSITYLow` <dbl>,
# `fSEASONWinter:DENSITYLow` <dbl>, reciprocal_dispersion <dbl>,
# accept_stat__ <dbl>, stepsize__ <dbl>, treedepth__ <dbl>,
# n_leapfrog__ <dbl>, divergent__ <dbl>, energy__ <dbl>
# A tibble: 1,800 × 11
.chain .iteration .draw `(Intercept)` fSEASONSummer fSEASONAutumn
<int> <int> <int> <dbl> <dbl> <dbl>
1 1 1 1 2.38 1.60 0.221
2 1 2 2 2.23 1.28 0.956
3 1 3 3 2.55 1.60 0.514
4 1 4 4 2.46 1.72 0.389
5 1 5 5 2.39 1.02 0.0498
6 1 6 6 2.28 1.61 0.643
7 1 7 7 2.36 1.36 0.596
8 1 8 8 2.40 1.33 0.580
9 1 9 9 2.29 1.43 0.810
10 1 10 10 2.26 1.99 0.509
# ℹ 1,790 more rows
# ℹ 5 more variables: fSEASONWinter <dbl>, DENSITYLow <dbl>,
# `fSEASONSummer:DENSITYLow` <dbl>, `fSEASONAutumn:DENSITYLow` <dbl>,
# `fSEASONWinter:DENSITYLow` <dbl>
Warning: Method 'posterior_samples' is deprecated. Please see ?as_draws for
recommended alternatives.
# A tibble: 1,800 × 9
`(Intercept)` fSEASONSummer fSEASONAutumn fSEASONWinter DENSITYLow
<dbl> <dbl> <dbl> <dbl> <dbl>
1 2.38 1.60 0.221 -0.695 0.138
2 2.23 1.28 0.956 -0.523 0.0968
3 2.55 1.60 0.514 -0.890 -0.559
4 2.46 1.72 0.389 -0.827 -0.113
5 2.39 1.02 0.0498 -0.654 -0.194
6 2.28 1.61 0.643 -0.792 0.122
7 2.36 1.36 0.596 -0.381 0.156
8 2.40 1.33 0.580 -0.566 0.191
9 2.29 1.43 0.810 -0.823 0.211
10 2.26 1.99 0.509 -0.269 -0.0674
# ℹ 1,790 more rows
# ℹ 4 more variables: `fSEASONSummer:DENSITYLow` <dbl>,
# `fSEASONAutumn:DENSITYLow` <dbl>, `fSEASONWinter:DENSITYLow` <dbl>,
# reciprocal_dispersion <dbl>
The summary() method generates simple summaries (mean, standard deviation as well as 10, 50 and 90 percentiles).
Family: negbinomial
Links: mu = log; shape = identity
Formula: RECRUITS ~ fSEASON * DENSITY
Data: quinn (Number of observations: 42)
Draws: 3 chains, each with iter = 5000; warmup = 2500; thin = 5;
total post-warmup draws = 1500
Regression Coefficients:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
Intercept 2.41 0.20 2.04 2.83 1.00 1116
fSEASONSummer 1.41 0.25 0.90 1.87 1.00 1386
fSEASONAutumn 0.56 0.26 0.04 1.05 1.00 1311
fSEASONWinter -0.68 0.29 -1.25 -0.11 1.00 1507
DENSITYLow -0.06 0.26 -0.58 0.45 1.00 1220
fSEASONSummer:DENSITYLow -0.64 0.34 -1.27 0.03 1.00 1393
fSEASONAutumn:DENSITYLow -0.01 0.38 -0.74 0.78 1.00 1277
fSEASONWinter:DENSITYLow -0.62 0.48 -1.57 0.34 1.00 1548
Tail_ESS
Intercept 1097
fSEASONSummer 1501
fSEASONAutumn 1414
fSEASONWinter 1519
DENSITYLow 1340
fSEASONSummer:DENSITYLow 1347
fSEASONAutumn:DENSITYLow 1382
fSEASONWinter:DENSITYLow 1458
Further Distributional Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
shape 6.79 3.09 2.84 14.45 1.00 1468 1482
Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
Conclusions:
- the intercept represents the estimated mean of the first combination of Season (Spring) and Density (High). On the link scale this is 2.41
- the difference between Low and High adult density in spring is 1.41, although this is not significant
- the difference between Spring and Summer for High adult density is 0.56
- the difference between Spring and Autumn for High adult density is -0.68
- the difference between Spring and Winter for High adult density is -0.06
- if there were no interactions, we would expect the Low density Summer recruitment to be the additive of the main effects (Low and Summer). However, the modelled mean is 0.64 less than the additive effects would have expected. This value is significantly different to 0, indicating that there is evidence that the density effect in Summer is different to that in Spring.
- the density effect in Autumn and Winter were not found to be significantly different from what you would expect from an additive model.
quinn.brmsNB$fit |>
tidyMCMC(
estimate.method = "median",
conf.int = TRUE,
conf.method = "HPDinterval",
rhat = TRUE,
ess = TRUE
)# A tibble: 11 × 7
term estimate std.error conf.low conf.high rhat ess
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
1 b_Intercept 2.41 0.196 2.01 2.79 1.00 1108
2 b_fSEASONSummer 1.41 0.255 0.900 1.87 0.999 1368
3 b_fSEASONAutumn 0.559 0.261 0.0407 1.05 1.00 1317
4 b_fSEASONWinter -0.677 0.287 -1.28 -0.149 1.00 1517
5 b_DENSITYLow -0.0554 0.257 -0.532 0.483 1.00 1206
6 b_fSEASONSummer:DENSITYLow -0.644 0.342 -1.27 0.0317 0.999 1400
7 b_fSEASONAutumn:DENSITYLow -0.00695 0.375 -0.689 0.802 1.00 1268
8 b_fSEASONWinter:DENSITYLow -0.623 0.484 -1.63 0.257 0.999 1542
9 shape 6.79 3.09 2.37 12.7 1.00 1433
10 Intercept 2.64 0.0816 2.47 2.79 1.000 1555
11 lprior -16.5 0.825 -18.1 -14.9 1.00 1393
Conclusions:
see above
# A draws_df: 500 iterations, 3 chains, and 12 variables
b_Intercept b_fSEASONSummer b_fSEASONAutumn b_fSEASONWinter b_DENSITYLow
1 2.7 1.16 0.520 -0.71 -0.170
2 2.8 1.04 0.065 -0.94 -0.314
3 2.8 1.09 0.305 -0.97 -1.017
4 2.9 1.11 0.379 -0.54 -0.797
5 2.5 1.25 0.567 -1.28 -0.299
6 2.8 1.09 -0.046 -0.84 -0.224
7 2.5 1.38 0.135 -0.65 0.022
8 2.8 0.96 0.230 -1.24 -0.304
9 2.3 1.51 0.895 -0.51 0.093
10 2.6 1.43 0.645 -1.02 -0.177
b_fSEASONSummer:DENSITYLow b_fSEASONAutumn:DENSITYLow
1 -0.47 0.464
2 -0.29 0.311
3 0.47 0.819
4 0.18 0.225
5 -0.46 -0.037
6 -0.79 0.342
7 -0.40 0.177
8 -0.29 0.191
9 -0.86 -0.234
10 -1.19 -0.366
b_fSEASONWinter:DENSITYLow
1 -0.44
2 -0.39
3 0.54
4 -0.93
5 -0.23
6 -0.21
7 -0.53
8 -0.90
9 -1.96
10 -0.18
# ... with 1490 more draws, and 4 more variables
# ... hidden reserved variables {'.chain', '.iteration', '.draw'}
quinn.brmsNB |>
as_draws_df() |>
summarise_draws(
median,
~ HDInterval::hdi(.x),
rhat,
ess_bulk, ess_tail
)# A tibble: 12 × 7
variable median lower upper rhat ess_bulk ess_tail
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 b_Intercept 2.41e+0 2.01e+0 2.79e+0 1.00 1117. 1097.
2 b_fSEASONSummer 1.42e+0 9.00e-1 1.87e+0 0.999 1386. 1501.
3 b_fSEASONAutumn 5.62e-1 4.07e-2 1.05e+0 1.00 1310. 1414.
4 b_fSEASONWinter -6.85e-1 -1.28e+0 -1.49e-1 1.00 1507. 1519.
5 b_DENSITYLow -4.84e-2 -5.32e-1 4.83e-1 1.00 1219. 1340.
6 b_fSEASONSummer:DENSITYLow -6.49e-1 -1.27e+0 3.17e-2 0.999 1393. 1347.
7 b_fSEASONAutumn:DENSITYLow -1.14e-2 -6.89e-1 8.02e-1 1.00 1277. 1382.
8 b_fSEASONWinter:DENSITYLow -6.28e-1 -1.63e+0 2.57e-1 1.00 1548. 1458.
9 shape 6.15e+0 2.37e+0 1.27e+1 1.00 1468. 1482.
10 Intercept 2.64e+0 2.47e+0 2.79e+0 1.000 1567. 1453.
11 lprior -1.64e+1 -1.81e+1 -1.49e+1 1.00 1406. 1428.
12 lp__ -1.57e+2 -1.62e+2 -1.53e+2 1.00 1442. 1419.
quinn.brmsNB |>
as_draws_df() |>
exp() |>
summarise_draws(
median,
HDInterval::hdi,
rhat,
length,
ess_bulk, ess_tail
)# A tibble: 12 × 8
variable median lower upper rhat length ess_bulk ess_tail
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 b_Intercept 1.11e+ 1 7.32e+ 0 1.61e+ 1 1.00 1500 1117. 1097.
2 b_fSEASONSummer 4.12e+ 0 2.34e+ 0 6.30e+ 0 0.999 1500 1386. 1501.
3 b_fSEASONAutumn 1.75e+ 0 9.63e- 1 2.73e+ 0 1.00 1500 1310. 1414.
4 b_fSEASONWinter 5.04e- 1 2.55e- 1 8.22e- 1 1.00 1500 1507. 1519.
5 b_DENSITYLow 9.53e- 1 5.05e- 1 1.48e+ 0 1.00 1500 1219. 1340.
6 b_fSEASONSummer:DE… 5.23e- 1 2.63e- 1 9.93e- 1 0.999 1500 1393. 1347.
7 b_fSEASONAutumn:DE… 9.89e- 1 4.28e- 1 1.95e+ 0 1.00 1500 1277. 1382.
8 b_fSEASONWinter:DE… 5.34e- 1 1.54e- 1 1.19e+ 0 1.00 1500 1548. 1458.
9 shape 4.69e+ 2 5.25e+ 0 2.46e+ 5 1.00 1500 1468. 1482.
10 Intercept 1.41e+ 1 1.18e+ 1 1.63e+ 1 1.000 1500 1567. 1453.
11 lprior 7.65e- 8 3.68e- 9 2.29e- 7 1.00 1500 1406. 1428.
12 lp__ 9.12e-69 3.97e-73 1.47e-67 1.00 1500 1442. 1419.
Due to the presence of a log transform in the predictor, it is better to use the regex version.
[1] "b_Intercept" "b_fSEASONSummer"
[3] "b_fSEASONAutumn" "b_fSEASONWinter"
[5] "b_DENSITYLow" "b_fSEASONSummer:DENSITYLow"
[7] "b_fSEASONAutumn:DENSITYLow" "b_fSEASONWinter:DENSITYLow"
[9] "shape" "Intercept"
[11] "lprior" "lp__"
[13] "accept_stat__" "stepsize__"
[15] "treedepth__" "n_leapfrog__"
[17] "divergent__" "energy__"
quinn.draw <- quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE)
quinn.draw# A tibble: 10,500 × 5
# Groups: .variable [7]
.chain .iteration .draw .variable .value
<int> <int> <int> <chr> <dbl>
1 1 1 1 b_fSEASONSummer 1.16
2 1 2 2 b_fSEASONSummer 1.04
3 1 3 3 b_fSEASONSummer 1.09
4 1 4 4 b_fSEASONSummer 1.11
5 1 5 5 b_fSEASONSummer 1.25
6 1 6 6 b_fSEASONSummer 1.09
7 1 7 7 b_fSEASONSummer 1.38
8 1 8 8 b_fSEASONSummer 0.957
9 1 9 9 b_fSEASONSummer 1.51
10 1 10 10 b_fSEASONSummer 1.43
# ℹ 10,490 more rows
quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
mutate(.value = exp(.value)) |>
summarise_draws(
median,
~ HDInterval::hdi(.x, credMass = 0.95),
rhat,
length,
ess_bulk, ess_tail
)# A tibble: 7 × 9
# Groups: .variable [7]
.variable variable median lower upper rhat length ess_bulk ess_tail
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 b_DENSITYLow .value 0.953 0.505 1.48 1.00 1500 1219. 1340.
2 b_fSEASONAutumn .value 1.75 0.963 2.73 1.00 1500 1310. 1414.
3 b_fSEASONAutumn:DE… .value 0.989 0.428 1.95 1.00 1500 1277. 1382.
4 b_fSEASONSummer .value 4.12 2.34 6.30 0.999 1500 1386. 1501.
5 b_fSEASONSummer:DE… .value 0.523 0.263 0.993 0.999 1500 1393. 1347.
6 b_fSEASONWinter .value 0.504 0.255 0.822 1.00 1500 1507. 1519.
7 b_fSEASONWinter:DE… .value 0.534 0.154 1.19 1.00 1500 1548. 1458.
exceedP <- function(x, Val = 0) mean(x > Val)
quinn.brmsNB |>
tidy_draws() |>
exp() |>
dplyr::select(starts_with("b_")) |>
summarise_draws(
median,
~ HDInterval::hdi(.x, credMass = 0.9),
rhat,
ess_bulk, ess_tail,
~ exceedP(.x, 1)
)# A tibble: 8 × 8
variable median lower upper rhat ess_bulk ess_tail `~exceedP(.x, 1)`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 b_Intercept 11.1 7.46 14.7 1.000 1100. 975. 1
2 b_fSEASONSummer 4.12 2.46 5.86 1.000 1346. 1460. 1
3 b_fSEASONAutumn 1.75 1.10 2.58 1.000 1303. 1408. 0.983
4 b_fSEASONWinter 0.504 0.292 0.769 0.999 1504. 1512. 0.0107
5 b_DENSITYLow 0.953 0.569 1.38 1.000 1210. 1332. 0.417
6 b_fSEASONSummer… 0.523 0.271 0.859 0.999 1391. 1253. 0.0347
7 b_fSEASONAutumn… 0.989 0.471 1.66 1.00 1274. 1378. 0.485
8 b_fSEASONWinter… 0.534 0.176 1.01 0.999 1537. 1453. 0.0947
We can then summarise this
# A tibble: 7 × 7
.variable .value .lower .upper .width .point .interval
<chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 b_DENSITYLow -0.0484 -0.532 0.483 0.95 median hdci
2 b_fSEASONAutumn 0.562 0.0407 1.05 0.95 median hdci
3 b_fSEASONAutumn:DENSITYLow -0.0114 -0.693 0.798 0.95 median hdci
4 b_fSEASONSummer 1.42 0.898 1.87 0.95 median hdci
5 b_fSEASONSummer:DENSITYLow -0.649 -1.30 0.00510 0.95 median hdci
6 b_fSEASONWinter -0.685 -1.30 -0.163 0.95 median hdci
7 b_fSEASONWinter:DENSITYLow -0.628 -1.63 0.257 0.95 median hdci
We could alternatively express the parameters on the response scale.
# A tibble: 7 × 7
.variable `exp(.value)` .lower .upper .width .point .interval
<chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 b_DENSITYLow 0.953 0.505 1.48 0.95 median hdci
2 b_fSEASONAutumn 1.75 0.992 2.78 0.95 median hdci
3 b_fSEASONAutumn:DENSITYLow 0.989 0.428 1.95 0.95 median hdci
4 b_fSEASONSummer 4.12 2.34 6.30 0.95 median hdci
5 b_fSEASONSummer:DENSITYLow 0.523 0.273 1.01 0.95 median hdci
6 b_fSEASONWinter 0.504 0.272 0.847 0.95 median hdci
7 b_fSEASONWinter:DENSITYLow 0.534 0.154 1.19 0.95 median hdci
quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
geom_vline(xintercept = 0, linetype = "dashed") +
stat_slab(aes(
x = .value, y = .variable,
fill = stat(ggdist::cut_cdf_qi(cdf,
.width = c(0.5, 0.8, 0.95),
labels = scales::percent_format()
))
), color = "black") +
scale_fill_brewer("Interval", direction = -1, na.translate = FALSE)quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
geom_vline(xintercept = 0, linetype = "dashed") +
stat_halfeye(aes(x = .value, y = .variable)) +
theme_classic()We could alternatively express the parameters on the response scale.
quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
group_by(.variable) |>
mutate(.value = exp(.value)) |>
median_hdci()# A tibble: 7 × 7
.variable .value .lower .upper .width .point .interval
<chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 b_DENSITYLow 0.953 0.505 1.48 0.95 median hdci
2 b_fSEASONAutumn 1.75 0.992 2.78 0.95 median hdci
3 b_fSEASONAutumn:DENSITYLow 0.989 0.428 1.95 0.95 median hdci
4 b_fSEASONSummer 4.12 2.34 6.30 0.95 median hdci
5 b_fSEASONSummer:DENSITYLow 0.523 0.273 1.01 0.95 median hdci
6 b_fSEASONWinter 0.504 0.272 0.847 0.95 median hdci
7 b_fSEASONWinter:DENSITYLow 0.534 0.154 1.19 0.95 median hdci
quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
mutate(.value = exp(.value)) |>
ggplot() +
geom_vline(xintercept = 1, linetype = "dashed") +
stat_slab(aes(
x = .value, y = .variable,
fill = stat(ggdist::cut_cdf_qi(cdf,
.width = c(0.5, 0.8, 0.95),
labels = scales::percent_format()
))
), color = "black") +
scale_x_continuous("", trans = scales::log2_trans(), breaks = unique(as.vector(2^(0:4 %o% c(-1, 1))))) +
scale_fill_brewer("Interval", direction = -1, na.translate = FALSE) +
theme_classic()Conclusions:
- the estimated mean (expected number of newly recruited barnacles) on the ALG1 surface is -0.05. This is the mean of the posterior distribution for this parameter. If we back-transform this to the response scale, this becomes 0.95.
- the estimated effect of ALG2 vs ALG1 is 0.56 (median) with a standard error of 0.04. The 95% credibility intervals indicate that we are 95% confident that the effect is between 1.05 and 0.95 - e.g. there is a significant positive effect. When back-transformed onto the response scale, we see that barnacle recruitment on ALG2 is 1.75 times higher than that on ALG1. This represents a 75% increase in barnacle recruitment.
- the estimated effect of NB and S are -0.01 and 1.42 respectively, which equate to 1.01 and 0.24 fold declines respectively.
- Rhat and number of effective samples for each parameter are also provided as MCMC diagnostics and all look good.
## Link scale
quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
stat_slab(aes(
x = .value, y = .variable,
fill = stat(ggdist::cut_cdf_qi(cdf,
.width = c(0.5, 0.8, 0.95),
labels = scales::percent_format()
))
), color = "black") +
geom_vline(xintercept = 0, linetype = "dashed") +
scale_fill_brewer("Interval", direction = -1, na.translate = FALSE)## Fractional scale
quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
mutate(.value = exp(.value)) |>
ggplot() +
stat_slab(aes(
x = .value, y = .variable,
fill = stat(ggdist::cut_cdf_qi(cdf,
.width = c(0.5, 0.8, 0.95),
labels = scales::percent_format()
))
), color = "black") +
geom_vline(xintercept = 1, linetype = "dashed") +
scale_fill_brewer("Interval", direction = -1, na.translate = FALSE) +
scale_x_continuous(trans = scales::log2_trans())quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
stat_halfeye(aes(x = .value, y = .variable)) +
facet_wrap(~.variable, scales = "free")quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
stat_halfeye(aes(x = .value, y = .variable)) +
geom_vline(xintercept = 0, linetype = "dashed")quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
stat_halfeye(aes(x = exp(.value), y = .variable)) +
geom_vline(xintercept = 1, linetype = "dashed") +
scale_x_continuous(trans = scales::log2_trans())quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
geom_density_ridges(aes(x = .value, y = .variable), alpha = 0.4) +
geom_vline(xintercept = 0, linetype = "dashed")Picking joint bandwidth of 0.0663
## Or on a fractional scale
quinn.brmsNB |>
gather_draws(`.Intercept.*|.*fSEASON.*|.*DENSITY.*`, regex = TRUE) |>
ggplot() +
geom_density_ridges_gradient(
aes(
x = exp(.value),
y = .variable,
fill = stat(x)
),
alpha = 0.4, colour = "white",
quantile_lines = TRUE,
quantiles = c(0.025, 0.975)
) +
geom_vline(xintercept = 1, linetype = "dashed") +
scale_x_continuous(trans = scales::log2_trans()) +
scale_fill_viridis_c(option = "C")Picking joint bandwidth of 0.0956
This is purely a graphical depiction on the posteriors.
# A tibble: 1,500 × 21
.chain .iteration .draw b_Intercept b_fSEASONSummer b_fSEASONAutumn
<int> <int> <int> <dbl> <dbl> <dbl>
1 1 1 1 2.72 1.16 0.520
2 1 2 2 2.81 1.04 0.0650
3 1 3 3 2.79 1.09 0.305
4 1 4 4 2.89 1.11 0.379
5 1 5 5 2.47 1.25 0.567
6 1 6 6 2.81 1.09 -0.0464
7 1 7 7 2.55 1.38 0.135
8 1 8 8 2.83 0.957 0.230
9 1 9 9 2.32 1.51 0.895
10 1 10 10 2.55 1.43 0.645
# ℹ 1,490 more rows
# ℹ 15 more variables: b_fSEASONWinter <dbl>, b_DENSITYLow <dbl>,
# `b_fSEASONSummer:DENSITYLow` <dbl>, `b_fSEASONAutumn:DENSITYLow` <dbl>,
# `b_fSEASONWinter:DENSITYLow` <dbl>, shape <dbl>, Intercept <dbl>,
# lprior <dbl>, lp__ <dbl>, accept_stat__ <dbl>, stepsize__ <dbl>,
# treedepth__ <dbl>, n_leapfrog__ <dbl>, divergent__ <dbl>, energy__ <dbl>
# A tibble: 1,500 × 10
.chain .iteration .draw b_fSEASONSummer b_fSEASONAutumn b_fSEASONWinter
<int> <int> <int> <dbl> <dbl> <dbl>
1 1 1 1 1.16 0.520 -0.713
2 1 2 2 1.04 0.0650 -0.945
3 1 3 3 1.09 0.305 -0.974
4 1 4 4 1.11 0.379 -0.541
5 1 5 5 1.25 0.567 -1.28
6 1 6 6 1.09 -0.0464 -0.837
7 1 7 7 1.38 0.135 -0.646
8 1 8 8 0.957 0.230 -1.24
9 1 9 9 1.51 0.895 -0.512
10 1 10 10 1.43 0.645 -1.02
# ℹ 1,490 more rows
# ℹ 4 more variables: b_DENSITYLow <dbl>, `b_fSEASONSummer:DENSITYLow` <dbl>,
# `b_fSEASONAutumn:DENSITYLow` <dbl>, `b_fSEASONWinter:DENSITYLow` <dbl>
Warning: Method 'posterior_samples' is deprecated. Please see ?as_draws for
recommended alternatives.
# A tibble: 1,500 × 12
b_Intercept b_fSEASONSummer b_fSEASONAutumn b_fSEASONWinter b_DENSITYLow
<dbl> <dbl> <dbl> <dbl> <dbl>
1 2.72 1.16 0.520 -0.713 -0.170
2 2.81 1.04 0.0650 -0.945 -0.314
3 2.79 1.09 0.305 -0.974 -1.02
4 2.89 1.11 0.379 -0.541 -0.797
5 2.47 1.25 0.567 -1.28 -0.299
6 2.81 1.09 -0.0464 -0.837 -0.224
7 2.55 1.38 0.135 -0.646 0.0223
8 2.83 0.957 0.230 -1.24 -0.304
9 2.32 1.51 0.895 -0.512 0.0934
10 2.55 1.43 0.645 -1.02 -0.177
# ℹ 1,490 more rows
# ℹ 7 more variables: `b_fSEASONSummer:DENSITYLow` <dbl>,
# `b_fSEASONAutumn:DENSITYLow` <dbl>, `b_fSEASONWinter:DENSITYLow` <dbl>,
# shape <dbl>, Intercept <dbl>, lprior <dbl>, lp__ <dbl>
Region of Practical Equivalence
[1] 0.2754809
# Proportion of samples inside the ROPE [-0.28, 0.28]:
Parameter | inside ROPE
--------------------------------------
Intercept | 0.00 %
fSEASONSummer | 0.00 %
fSEASONAutumn | 12.64 %
fSEASONWinter | 5.69 %
DENSITYLow | 74.30 %
fSEASONSummer:DENSITYLow | 12.01 %
fSEASONAutumn:DENSITYLow | 57.65 %
fSEASONWinter:DENSITYLow | 20.37 %
## Or based on fractional scale
quinn.brmsNB |>
as_draws_df("^b_fSEASON.*|^b_DENSITY.*", regex = TRUE) |>
exp() |>
## equivalence_test(range = c(0.755, 1.32))
rope(range = c(0.755, 1.32))# Proportion of samples inside the ROPE [0.76, 1.32]:
Parameter | inside ROPE
--------------------------------------
fSEASONSummer | 0.00 %
fSEASONAutumn | 12.36 %
fSEASONWinter | 5.83 %
DENSITYLow | 74.44 %
fSEASONSummer:DENSITYLow | 12.08 %
fSEASONAutumn:DENSITYLow | 57.58 %
fSEASONWinter:DENSITYLow | 20.58 %
quinn.mcmc <-
quinn.brmsNB |>
as_draws_df("^b_fSEASON.*|^b_DENSITY.*", regex = TRUE) |>
exp()
quinn.mcmc |>
rope(range = c(0.755, 1.32))# Proportion of samples inside the ROPE [0.76, 1.32]:
Parameter | inside ROPE
--------------------------------------
fSEASONSummer | 0.00 %
fSEASONAutumn | 12.36 %
fSEASONWinter | 5.83 %
DENSITYLow | 74.44 %
fSEASONSummer:DENSITYLow | 12.08 %
fSEASONAutumn:DENSITYLow | 57.58 %
fSEASONWinter:DENSITYLow | 20.58 %
## note, the following is not quit correct, it does not get the CI correct
quinn.mcmc |>
rope(range = c(0.755, 1.32)) |>
plot(data = quinn.brmsNB)# Test for Practical Equivalence
ROPE: [0.76 1.32]
Parameter | H0 | inside ROPE | 95% HDI
-----------------------------------------------------------------
fSEASONSummer | Rejected | 0.00 % | [2.46, 6.50]
fSEASONAutumn | Undecided | 12.36 % | [1.05, 2.86]
fSEASONWinter | Undecided | 5.83 % | [0.29, 0.89]
DENSITYLow | Undecided | 74.44 % | [0.56, 1.57]
fSEASONSummer:DENSITYLow | Undecided | 12.08 % | [0.28, 1.03]
fSEASONAutumn:DENSITYLow | Undecided | 57.58 % | [0.47, 2.18]
fSEASONWinter:DENSITYLow | Undecided | 20.58 % | [0.21, 1.40]
quinn.brmsNB |> modelsummary(
statistic = c("conf.low", "conf.high"),
shape = term ~ statistic,
exponentiate = TRUE
)Warning:
`modelsummary` uses the `performance` package to extract goodness-of-fit
statistics from models of this class. You can specify the statistics you wish
to compute by supplying a `metrics` argument to `modelsummary`, which will then
push it forward to `performance`. Acceptable values are: "all", "common",
"none", or a character vector of metrics names. For example: `modelsummary(mod,
metrics = c("RMSE", "R2")` Note that some metrics are computationally
expensive. See `?performance::performance` for details.
This warning appears once per session.
| (1) | |||
|---|---|---|---|
| Est. | 2.5 % | 97.5 % | |
| b_Intercept | 11.135 | 7.656 | 16.948 |
| b_fSEASONSummer | 4.122 | 2.456 | 6.502 |
| b_fSEASONAutumn | 1.754 | 1.045 | 2.861 |
| b_fSEASONWinter | 0.504 | 0.286 | 0.892 |
| b_DENSITYLow | 0.953 | 0.561 | 1.573 |
| b_fSEASONSummer × DENSITYLow | 0.523 | 0.280 | 1.029 |
| b_fSEASONAutumn × DENSITYLow | 0.989 | 0.475 | 2.179 |
| b_fSEASONWinter × DENSITYLow | 0.534 | 0.208 | 1.403 |
| Num.Obs. | 42 | ||
| R2 | 0.727 | ||
| ELPD | -148.4 | ||
| ELPD s.e. | 6.1 | ||
| LOOIC | 296.8 | ||
| LOOIC s.e. | 12.1 | ||
| WAIC | 295.9 | ||
| RMSE | 7.54 | ||
11 Further investigations
fSEASON = Spring:
contrast ratio lower.HPD upper.HPD
High / Low 0.892 0.311 1.67
fSEASON = Summer:
contrast ratio lower.HPD upper.HPD
High / Low 2.175 1.033 3.77
fSEASON = Autumn:
contrast ratio lower.HPD upper.HPD
High / Low 1.051 0.469 2.03
fSEASON = Winter:
contrast ratio lower.HPD upper.HPD
High / Low 2.106 0.457 5.64
Point estimate displayed: median
Results are back-transformed from the log scale
HPD interval probability: 0.95
## absolute response scale
quinn.rstanarmNB |>
emmeans(~ DENSITY | fSEASON, type = "link") |>
regrid() |>
pairs()fSEASON = Spring:
contrast estimate lower.HPD upper.HPD
High - Low -1.233 -9.768 7.24
fSEASON = Summer:
contrast estimate lower.HPD upper.HPD
High - Low 25.594 4.111 53.14
fSEASON = Autumn:
contrast estimate lower.HPD upper.HPD
High - Low 0.924 -13.659 17.31
fSEASON = Winter:
contrast estimate lower.HPD upper.HPD
High - Low 2.881 -0.856 8.00
Point estimate displayed: median
HPD interval probability: 0.95
quinn.em <- quinn.rstanarmNB |>
emmeans(~ DENSITY | fSEASON, type = "link") |>
pairs() |>
gather_emmeans_draws() |>
mutate(Fit = exp(.value))
head(quinn.em)# A tibble: 6 × 7
# Groups: contrast, fSEASON [1]
contrast fSEASON .chain .iteration .draw .value Fit
<fct> <fct> <int> <int> <int> <dbl> <dbl>
1 High - Low Spring NA NA 1 -0.138 0.871
2 High - Low Spring NA NA 2 -0.0968 0.908
3 High - Low Spring NA NA 3 0.559 1.75
4 High - Low Spring NA NA 4 0.113 1.12
5 High - Low Spring NA NA 5 0.194 1.21
6 High - Low Spring NA NA 6 -0.122 0.885
g2 <- quinn.em |>
group_by(contrast, fSEASON) |>
median_hdci() |>
ggplot() +
geom_vline(xintercept = 1, linetype = "dashed") +
geom_pointrange(aes(x = Fit, y = fSEASON, xmin = Fit.lower, xmax = Fit.upper)) +
scale_x_continuous("Effect size (High/Low)", trans = scales::log2_trans(), breaks = unique(as.vector(2^(0:4 %o% c(-1, 1))))) +
theme_classic()
g2ggplot(quinn.em, aes(x = Fit)) +
geom_histogram() +
geom_vline(xintercept = 1, linetype = "dashed") +
scale_x_continuous("Effect size (High/Low)", trans = scales::log2_trans(), breaks = unique(as.vector(2^(0:4 %o% c(-1, 1))))) +
facet_wrap(fSEASON ~ contrast, scales = "free")`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# A tibble: 4 × 8
contrast fSEASON Fit .lower .upper .width .point .interval
<fct> <fct> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 High - Low Spring 0.892 0.361 1.73 0.95 median hdci
2 High - Low Summer 2.17 1.03 3.77 0.95 median hdci
3 High - Low Autumn 1.05 0.460 2.02 0.95 median hdci
4 High - Low Winter 2.11 0.457 5.64 0.95 median hdci
`summarise()` has grouped output by 'contrast'. You can override using the
`.groups` argument.
# A tibble: 4 × 3
# Groups: contrast [1]
contrast fSEASON P
<fct> <fct> <dbl>
1 High - Low Spring 0.364
2 High - Low Summer 0.988
3 High - Low Autumn 0.546
4 High - Low Winter 0.926
## Probability of effect greater than 10%
quinn.em |>
group_by(contrast, fSEASON) |>
summarize(P = mean(Fit > 1.1))`summarise()` has grouped output by 'contrast'. You can override using the
`.groups` argument.
# A tibble: 4 × 3
# Groups: contrast [1]
contrast fSEASON P
<fct> <fct> <dbl>
1 High - Low Spring 0.277
2 High - Low Summer 0.981
3 High - Low Autumn 0.451
4 High - Low Winter 0.9
## Using summarise_draws
quinn.rstanarmNB |>
emmeans(~ DENSITY | fSEASON, type = "link") |>
pairs() |>
tidy_draws() |>
exp() |>
summarise_draws(median,
HDInterval::hdi,
P = ~ mean(.x > 1)
)# A tibble: 4 × 5
variable median lower upper P
<chr> <dbl> <dbl> <dbl> <dbl>
1 contrast High - Low fSEASON Spring 0.892 0.311 1.67 0.364
2 contrast High - Low fSEASON Summer 2.17 1.03 3.77 0.988
3 contrast High - Low fSEASON Autumn 1.05 0.469 2.03 0.546
4 contrast High - Low fSEASON Winter 2.11 0.457 5.64 0.926
newdata <- with(quinn, expand.grid(
fSEASON = levels(fSEASON),
DENSITY = levels(DENSITY)
))
Xmat <- model.matrix(~ fSEASON * DENSITY, data = newdata)
as.matrix(quinn.rstanarmNB) |> head() parameters
iterations (Intercept) fSEASONSummer fSEASONAutumn fSEASONWinter DENSITYLow
[1,] 2.381146 1.603036 0.22101808 -0.6949756 0.13761188
[2,] 2.226240 1.278036 0.95572540 -0.5233653 0.09676273
[3,] 2.553322 1.604023 0.51357944 -0.8898770 -0.55918503
[4,] 2.462228 1.722616 0.38875020 -0.8272478 -0.11262107
[5,] 2.387923 1.023239 0.04979926 -0.6542586 -0.19358063
[6,] 2.279176 1.606230 0.64332711 -0.7916412 0.12224754
parameters
iterations fSEASONSummer:DENSITYLow fSEASONAutumn:DENSITYLow
[1,] -1.1843259 0.40104431
[2,] -0.8951184 -0.50843028
[3,] -0.4686581 0.06325034
[4,] -1.1148374 -0.10442549
[5,] -0.2206245 0.84074413
[6,] -0.5920347 -0.42434611
parameters
iterations fSEASONWinter:DENSITYLow reciprocal_dispersion
[1,] -0.21297875 3.692986
[2,] -1.03336169 4.445364
[3,] -0.10664788 5.414934
[4,] -0.61580878 5.067576
[5,] -0.76853431 2.328569
[6,] -0.04352769 3.060953
## coefs <- as.matrix(quinn.rstanarmNB)
coefs <- as.matrix(as.data.frame(quinn.rstanarmNB) |>
dplyr:::select(-reciprocal_dispersion)) |>
as.matrix()
fit <- exp(coefs %*% t(Xmat))
newdata <- newdata |>
cbind(tidyMCMC(fit, conf.int = TRUE, conf.method = "HPDinterval"))
head(newdata) fSEASON DENSITY term estimate std.error conf.low conf.high
1 Spring High 1 10.508575 2.723153 5.696427 15.90726
2 Summer High 2 49.905997 11.822240 30.040811 72.23297
3 Autumn High 3 20.430422 5.016132 11.575267 30.16747
4 Winter High 4 5.915299 1.703658 3.054870 9.15440
5 Spring Low 5 11.858948 3.351021 6.102056 18.10083
6 Summer Low 6 23.176212 5.651388 14.416983 35.28266
ggplot(newdata, aes(y = estimate, x = fSEASON, fill = DENSITY)) +
geom_blank() +
geom_line(aes(x = as.numeric(fSEASON), ymin = conf.low, ymax = conf.high, linetype = DENSITY),
position = position_dodge(0.2)
) +
geom_pointrange(aes(ymin = conf.low, ymax = conf.high),
shape = 21,
position = position_dodge(0.2)
)Warning in geom_line(aes(x = as.numeric(fSEASON), ymin = conf.low, ymax =
conf.high, : Ignoring unknown aesthetics: ymin and ymax
# Compare high and low in each season
# via contrasts
newdata <- with(quinn, expand.grid(
fSEASON = levels(fSEASON),
DENSITY = levels(DENSITY)
))
## factor differences
Xmat <- model.matrix(~ fSEASON * DENSITY, data = newdata)
Xmat.high <- Xmat[newdata$DENSITY == "High", ]
Xmat.low <- Xmat[newdata$DENSITY == "Low", ]
Xmat.density <- Xmat.high - Xmat.low
rownames(Xmat.density) <- levels(quinn$fSEASON)
coefs <- as.matrix(as.data.frame(quinn.rstanarmNB) |> dplyr:::select(-reciprocal_dispersion))
fit <- exp(coefs %*% t(Xmat.density))
tidyMCMC(fit, conf.int = TRUE, conf.method = "HPDinterval")# A tibble: 4 × 5
term estimate std.error conf.low conf.high
<chr> <dbl> <dbl> <dbl> <dbl>
1 Spring 0.952 0.358 0.311 1.67
2 Summer 2.27 0.743 1.03 3.77
3 Autumn 1.13 0.433 0.469 2.03
4 Winter 2.53 1.55 0.457 5.64
## or absolute
fit.high <- coefs %*% t(Xmat.high)
fit.low <- coefs %*% t(Xmat.low)
fit <- exp(fit.high) - exp(fit.low)
# fit = exp(fit.high - fit.low)
tidyMCMC(fit, conf.int = TRUE, conf.method = "HPDinterval")# A tibble: 4 × 5
term estimate std.error conf.low conf.high
<chr> <dbl> <dbl> <dbl> <dbl>
1 1 -1.35 4.32 -9.77 7.24
2 2 26.7 13.1 4.11 53.1
3 3 0.700 7.97 -13.7 17.3
4 4 2.96 2.23 -0.856 8.00
Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
fSEASON = Spring:
contrast ratio lower.HPD upper.HPD
High / Low 1.05 0.599 1.66
fSEASON = Summer:
contrast ratio lower.HPD upper.HPD
High / Low 2.00 1.043 3.10
fSEASON = Autumn:
contrast ratio lower.HPD upper.HPD
High / Low 1.06 0.571 1.78
fSEASON = Winter:
contrast ratio lower.HPD upper.HPD
High / Low 1.98 0.686 4.29
Point estimate displayed: median
Results are back-transformed from the log scale
HPD interval probability: 0.95
## absolute response scale
quinn.brmsNB |>
emmeans(~ DENSITY | fSEASON, type = "link") |>
regrid() |>
pairs()Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
fSEASON = Spring:
contrast estimate lower.HPD upper.HPD
High - Low 0.539 -5.303 6.19
fSEASON = Summer:
contrast estimate lower.HPD upper.HPD
High - Low 22.574 3.322 40.96
fSEASON = Autumn:
contrast estimate lower.HPD upper.HPD
High - Low 1.152 -9.736 11.96
fSEASON = Winter:
contrast estimate lower.HPD upper.HPD
High - Low 2.735 -0.765 6.31
Point estimate displayed: median
HPD interval probability: 0.95
quinn.em <- quinn.brmsNB |>
emmeans(~ DENSITY | fSEASON, type = "link") |>
pairs() |>
gather_emmeans_draws() |>
mutate(Fit = exp(.value))Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
# A tibble: 6 × 7
# Groups: contrast, fSEASON [1]
contrast fSEASON .chain .iteration .draw .value Fit
<fct> <fct> <int> <int> <int> <dbl> <dbl>
1 High - Low Spring NA NA 1 0.170 1.19
2 High - Low Spring NA NA 2 0.314 1.37
3 High - Low Spring NA NA 3 1.02 2.76
4 High - Low Spring NA NA 4 0.797 2.22
5 High - Low Spring NA NA 5 0.299 1.35
6 High - Low Spring NA NA 6 0.224 1.25
g2 <- quinn.em |>
group_by(contrast, fSEASON) |>
median_hdci() |>
ggplot() +
geom_vline(xintercept = 1, linetype = "dashed") +
geom_pointrange(aes(x = Fit, y = fSEASON, xmin = Fit.lower, xmax = Fit.upper)) +
scale_x_continuous("Effect size (High/Low)", trans = scales::log2_trans(), breaks = unique(as.vector(2^(0:4 %o% c(-1, 1))))) +
theme_classic()
g2ggplot(quinn.em, aes(x = Fit)) +
geom_histogram() +
geom_vline(xintercept = 1, linetype = "dashed") +
scale_x_continuous("Effect size (High/Low)", trans = scales::log2_trans(), breaks = unique(as.vector(2^(0:4 %o% c(-1, 1))))) +
facet_wrap(fSEASON ~ contrast, scales = "free")`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# A tibble: 4 × 8
contrast fSEASON Fit .lower .upper .width .point .interval
<fct> <fct> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 High - Low Spring 1.05 0.599 1.66 0.95 median hdci
2 High - Low Summer 2.00 1.08 3.15 0.95 median hdci
3 High - Low Autumn 1.06 0.571 1.78 0.95 median hdci
4 High - Low Winter 1.98 0.686 4.29 0.95 median hdci
`summarise()` has grouped output by 'contrast'. You can override using the
`.groups` argument.
# A tibble: 4 × 3
# Groups: contrast [1]
contrast fSEASON P
<fct> <fct> <dbl>
1 High - Low Spring 0.583
2 High - Low Summer 0.995
3 High - Low Autumn 0.585
4 High - Low Winter 0.933
## Probability of effect greater than 10%
quinn.em |>
group_by(contrast, fSEASON) |>
summarize(P = mean(Fit > 1.1))`summarise()` has grouped output by 'contrast'. You can override using the
`.groups` argument.
# A tibble: 4 × 3
# Groups: contrast [1]
contrast fSEASON P
<fct> <fct> <dbl>
1 High - Low Spring 0.435
2 High - Low Summer 0.986
3 High - Low Autumn 0.453
4 High - Low Winter 0.901
12 Summary figures
newdata <- quinn.rstanarmNB |>
emmeans(~ fSEASON | DENSITY, type = "response") |>
as.data.frame()
head(newdata) fSEASON DENSITY prob lower.HPD upper.HPD
Spring High 10.11729 5.696427 15.90726
Summer High 48.05665 30.040811 72.23297
Autumn High 19.84166 11.575267 30.16747
Winter High 5.69321 3.054870 9.15440
Spring Low 11.38619 6.102056 18.10083
Summer Low 22.40605 14.416983 35.28266
Point estimate displayed: median
Results are back-transformed from the log scale
HPD interval probability: 0.95
g1 <- ggplot(newdata, aes(y = prob, x = fSEASON, color = DENSITY)) +
geom_pointrange(aes(ymin = lower.HPD, ymax = upper.HPD),
position = position_dodge(width = 0.2)
) +
theme_classic()
g1 + g2Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
fSEASON DENSITY prob lower.HPD upper.HPD
Spring High 11.13501 7.322182 16.08760
Summer High 45.59039 31.359375 64.08547
Autumn High 19.42992 13.214126 27.35445
Winter High 5.63679 3.465218 8.44709
Spring Low 10.66957 6.686198 15.37904
Summer Low 22.68431 15.127838 32.66172
Point estimate displayed: median
Results are back-transformed from the log scale
HPD interval probability: 0.95
g1 <- ggplot(newdata, aes(y = prob, x = fSEASON, color = DENSITY)) +
geom_pointrange(aes(ymin = lower.HPD, ymax = upper.HPD),
position = position_dodge(width = 0.2)
) +
theme_classic()
g1 + g213 Observation-level random effects
13.1 brms
quinn <- quinn |>
group_by(fSEASON, DENSITY) |>
mutate(Obs = factor(1:n()))
quinn.form <- bf(RECRUITS ~ fSEASON * DENSITY + (1 | Obs), family = poisson(link = "log"))
get_prior(quinn.form, data = quinn) prior class coef group resp dpar
(flat) b
(flat) b DENSITYLow
(flat) b fSEASONAutumn
(flat) b fSEASONAutumn:DENSITYLow
(flat) b fSEASONSummer
(flat) b fSEASONSummer:DENSITYLow
(flat) b fSEASONWinter
(flat) b fSEASONWinter:DENSITYLow
student_t(3, 2.6, 2.5) Intercept
student_t(3, 0, 2.5) sd
student_t(3, 0, 2.5) sd Obs
student_t(3, 0, 2.5) sd Intercept Obs
nlpar lb ub source
default
(vectorized)
(vectorized)
(vectorized)
(vectorized)
(vectorized)
(vectorized)
(vectorized)
default
0 default
0 (vectorized)
0 (vectorized)
quinn.brmsU <- brm(quinn.form,
data = quinn,
refresh = 0,
chains = 3,
iter = 5000,
thin = 5,
warmup = 2000
)Compiling Stan program...
Start sampling
Warning: Argument 'nsamples' is deprecated. Please use argument 'ndraws'
instead.
quinn.resids <- createDHARMa(
simulatedResponse = t(preds),
observedResponse = quinn$RECRUITS,
fittedPredictedResponse = apply(preds, 2, median),
integerResponse = TRUE
)
plot(quinn.resids)Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
DENSITY = High:
fSEASON rate lower.HPD upper.HPD
Spring 9.76608 6.61843 13.61163
Summer 47.15867 34.03303 62.28887
Autumn 19.23758 12.81330 25.54698
Winter 5.50793 3.22782 7.96708
DENSITY = Low:
fSEASON rate lower.HPD upper.HPD
Spring 10.84652 7.23020 15.45789
Summer 21.49778 14.60513 28.04147
Autumn 16.29993 10.60382 22.41411
Winter 2.39485 0.90701 4.50590
Point estimate displayed: median
Results are back-transformed from the log scale
HPD interval probability: 0.95
ggplot(newdata, aes(y = rate, x = fSEASON, color = DENSITY)) +
geom_pointrange(aes(ymin = lower.HPD, ymax = upper.HPD),
position = position_dodge(width = 0.2)
)